arcgis python 沿线生成点
# coding: utf-8
"""
Source Name: generatepointsfromlines.py
Version: ArcGIS 10.4/Pro 1.2
Author: Environmental Systems Research Institute Inc.
Description: Source for Generate Points From Line geoprocessing tool.
""" import arcpy
import os
from collections import namedtuple unit_conversion = dict(CENTIMETERS=0.01, DECIMETERS=0.1, FEET=0.3048,
INCHES=0.0254, KILOMETERS=1000.0, METERS=1.0,
MILES=1609.347218694438, MILLIMETERS=0.001,
NAUTICALMILES=1852.0, POINTS=0.000352777778,
UNKNOWN=1.0, YARDS=0.9144) point_placement = dict(DISTANCE=False, PERCENTAGE=True) def create_points_from_lines(input_fc, output_fc, spatial_ref, percent=False,
dist=True, add_end_points=False):
"""Convert line features to feature class of points :param input_fc: Input line features
:param output_fc: Output point feature class
:param spatial_ref: The spatial reference of the input
:param percent: If creating points by percentage (or distance)
:param dist: The distance used to create points (if percentage == False).
The distance should be in units of the input (see convert_units)
:param add_end_points: If True, an extra point will be added from start
and end point of each line feature
:return: None
""" if percent:
is_percentage = True
else:
is_percentage = False # Create output feature class
arcpy.CreateFeatureclass_management(
os.path.dirname(output_fc),
os.path.basename(output_fc),
geometry_type="POINT",
spatial_reference=spatial_ref) # Add a field to transfer FID from input
fid_name = 'ORIG_FID'
arcpy.AddField_management(output_fc, fid_name, 'LONG') # Create new points based on input lines
in_fields = ['SHAPE@', 'OID@']
out_fields = ['SHAPE@', fid_name] with arcpy.da.SearchCursor(input_fc, in_fields) as search_cursor:
with arcpy.da.InsertCursor(output_fc, out_fields) as insert_cursor:
for row in search_cursor:
line = row[0] if line: # if null geometry--skip
if line.type == 'polygon':
line = line.boundary() if add_end_points:
insert_cursor.insertRow([line.firstPoint, row[1]]) increment = (percent or dist)
cur_length = increment if is_percentage:
max_position = 1.0
else:
max_position = line.length while cur_length < max_position:
new_point = line.positionAlongLine(cur_length,
is_percentage)
insert_cursor.insertRow([new_point, row[1]])
cur_length += increment if add_end_points:
end_point = line.positionAlongLine(1, True)
insert_cursor.insertRow([end_point, row[1]]) try:
arcpy.JoinField_management(out_fc,
fid_name,
input_fc,
arcpy.Describe(input_fc).OIDFieldName)
except arcpy.ExecuteError:
# In unlikely event that JoinField fails, proceed regardless,
# as spatial and join field are already complete
pass return def convert_units(dist, param_units, spatial_info):
"""Base unit conversion :param dist: Distance
:param param_units: The units as supplied from tool parameter
:param spatial_info: arcpy.SpatialReference object
:return: Distance after converted to new units
""" param_units = param_units.upper() if param_units in ['', None, 'UNKNOWN']:
return dist
else:
if param_units != 'DECIMALDEGREES':
p_conversion = unit_conversion[param_units]
else:
p_conversion = 111319.8 try:
sr_conversion = spatial_info.spatialReference.metersPerUnit
except AttributeError:
try:
input_extent = spatial_info.extent centroid = input_extent.polygon.centroid
point1 = centroid.Y, centroid.X - 0.5
point2 = centroid.Y, centroid.X + 0.5
sr_conversion = haversine(point1, point2) * 1000
except Exception as err:
# Fallback
sr_conversion = 111319.8 return dist * (p_conversion / sr_conversion) def get_distance_and_units(dist):
""" Pull distance and units from a linear unit. If units are not
specified, return UNKNOWN. :param dist: Linear units
:return: Tuple of distance (float) and units (string)
"""
try:
dist, units = dist.split(' ', 1)
except ValueError:
# ValueError occurs if units are not specified, use 'UNKNOWN'
units = 'UNKNOWN' dist = dist.replace(',', '.') return float(dist), units def haversine(point1, point2):
""" Calculate the distance between two points on the Earth surface around its curvature.
Does not account for changes in elevation (datum) :param point1 Tuple - Tuple of (Lat, Long) for the first point
:param point2 Tuple - Tuple of (Lat, Long) for the second point
:return Float - The distance between the two points about the surface of the globe in kilometers.
"""
from math import radians, sin, cos, asin, sqrt
radius_of_earth_km = 6371
lat1, lng1, lat2, lng2 = list(map(radians, list(point1 + point2)))
d = sin((lat2 - lat1) / 2) ** 2 + cos(lat1) * cos(lat2) * sin((lng2 - lng1) / 2) ** 2
return 2 * radius_of_earth_km * asin(sqrt(d)) if __name__ == '__main__':
in_features = arcpy.GetParameterAsText(0) # String
out_fc = arcpy.GetParameterAsText(1) # String
use_percent = point_placement[arcpy.GetParameter(2)] # Str -> Bool
end_points = arcpy.GetParameter(5) # Boolean describe = arcpy.Describe(in_features)
spatial_info = namedtuple('spatial_info', 'spatialReference extent')
sp_info = spatial_info(spatialReference=describe.spatialReference,
extent=describe.extent) if use_percent:
percentage = arcpy.GetParameter(4) / 100 # Float
create_points_from_lines(in_features, out_fc, sp_info.spatialReference,
percent=percentage, add_end_points=end_points)
else:
distance = arcpy.GetParameterAsText(3) # String
distance, param_linear_units = get_distance_and_units(distance)
distance = convert_units(distance, param_linear_units,
sp_info) create_points_from_lines(in_features, out_fc, sp_info.spatialReference,
dist=distance, add_end_points=end_points) try:
arcpy.AddSpatialIndex_management(out_fc)
except arcpy.ExecuteError:
pass
arcgis python 沿线生成点的更多相关文章
- ArcGIS Python人门到精通目录基于ArcGIS10.2,100以上案例15章42个视频806分钟,51GIS网站上线
ArcGIS Python人门到精通目录 闫老师 QQ:276529800 微信13108507190 1. ArcGIS Python基础 1.1 ArcGIS为什么学习Python 1.2 A ...
- arcgis python arcpy add data script添加数据脚本
arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...
- ArcGIS Python编程案例-电子资料链接
ArcGIS Python编程案例(1)-Python语言基础 https://www.jianshu.com/p/dd90816d019b ArcGIS Python编程案例(2)-使用ArcPy编 ...
- arcgis python脚本工具实例教程—栅格范围提取至多边形要素类
arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...
- arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算)
arcpy arcgis python实例教程--原点夹角距离定义线(坐标正算) 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 此地理处理工具 ...
- arcgis python获得字段唯一值
arcgis python获得字段唯一值 # Import native arcgisscripting moduleimport arcgisscripting, sys# Create the g ...
- ArcGis Python常用脚本
ArcGis Python脚本——ArcGIS 中使用的 Python 是什么版本 ArcGis Python脚本——批量添加字段 ArcGis Python脚本——批量删除字段 ArcGis Pyt ...
- 【ArcGIS遇上Python】ArcGIS Python批处理入门到精通实用教程目录
目录 1. 专栏简介 2. 专栏地址 3. 专栏目录 1. 专栏简介 Python语言是目前很火热的语言,极大的促进了人工智能发展.你知道在ArcGIS中也会有python的身影吗?事实上,在ArcG ...
- ArcGis Python脚本——遍历输出面或折线要素的折点坐标
有示例要素类如下 经过下面代码处理 #遍历输出面或折线要素的折点坐标 #infc:输入要素类 # code source: https://www.cnblogs.com/yzhyingcool/# ...
随机推荐
- 毕设demo写好
2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...
- C/C++经典面试题
1.指向数组的指针 和 指向数组首元素的指针 2018-03-07 昨天在牛客上看到这么一道C语言面试题,挺经典的,特来分享给大家. 程序如下,问输出结果 #include <stdio.h&g ...
- spring-boot分环境打包为war包
1.启动类修改 @EnableSwagger2 @SpringBootApplication public class CustWebAcApplication extends SpringBootS ...
- HBase(十)HBase性能调优总结
一. HBase的通用优化 1 高可用 在 HBase 中 Hmaster 负责监控 RegionServer 的生命周期,均衡 RegionServer 的负载,如果 Hmaster 挂掉了,那么整 ...
- 【LOJ】#2027. 「SHOI2016」黑暗前的幻想乡
题解 我一开始写的最小表示法写的插头dp,愉快地TLE成60分 然后我觉得我就去看正解了! 发现是容斥 + 矩阵树定理 矩阵树定理对于有重边的图只要邻接矩阵的边数设置a[u][v]表示u,v之间有几条 ...
- USACO 5.5 Hidden Password
Hidden Password ACM South Eastern Europe -- 2003 Sometimes the programmers have very strange ways of ...
- thinkphp3.2路由美化,url简化
thinkphp的路由功能很实用也很强大,可以简化url,有强大的正则匹配,可以做成任何想要的url样式. 在前台的config.php配置文件中: 1.首先开启路由 1 'URL_ROUTER_ON ...
- Django一些开发经验
总结一些 Django 开发的小经验.先说一些最最基础的吧. 使用 virtualenv 隔离开发环境 使用 pip 管理项目依赖,主要就是一个小技巧,使用 pip freeze > requi ...
- 深入理解ajax系列第六篇
前面的话 每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用.XHR对象提供了操作头部信息的方法.本文将详细介绍HTTP的头部信息 默认信息 默认情况下,在发送XHR请求的同时,还会 ...
- Linux嵌入式文件系统(网络文件系统)
<文件系统定义> 怎么将文件和文件目录加载到linux内核中,这一种加载的方式就叫做文件系统 <建立根文件系统目录和文件> <创建目录> 1)在linux系统中使用 ...