Arcpy神器之NumPyArrayToFeatureClass
Arcpy神器之NumPyArrayToFeatureClass
之前在“ofo数据获取&坐标转换&生成shp数据”一文中涉及到用Arcpy创建点要素数据,最近再次使用时发现怎么那么麻烦
于是有另辟新路,翻阅Arcpy接口文档,终于找到了一个神器NumPyArrayToFeatureClass。
我们先看看原来的写法
#创建shp点数据
def CreateFeaturclass(self, savepath, featurename, spatial):
if arcpy.Exists(savepath + '\\' + featurename + '.shp') == False:
arcpy.CreateFeatureclass_management(savepath, featurename, 'POINT', '', '', '', spatial)
else:
pass
#添加字段
def AddField(self, savepath, featurename):
arcpy.AddField_management(savepath + '\\' + featurename + '.shp', 'bikeid', 'TEXT')
arcpy.AddField_management(savepath + '\\' + featurename + '.shp', 'lon', 'TEXT')
arcpy.AddField_management(savepath + '\\' + featurename + '.shp', 'lat', 'TEXT')
#遍历点并添加字段值
def InsertRow(self, savepath, featurename, data):
Insercur = arcpy.InsertCursor(savepath + '\\' + featurename + '.shp')
for value in range(1,len(data)+1):
point = arcpy.Point()
newrow = Insercur.newRow()
point.X = float(data.head(value)['lng'][value-1])
point.Y = float(data.head(value)['lat'][value-1])
newrow.setValue('Id', value)
newrow.setValue('bikeid',data.head(value)['bikeId'][value-1])
newrow.setValue('lon',data.head(value)['lng'][value-1])
newrow.setValue('lat',data.head(value)['lat'][value-1])
pointGeo = arcpy.PointGeometry(point)
newrow.shape = pointGeo
Insercur.insertRow(newrow)
emmm…
这么多,于是,我将上面三个函数改一下,看看神器NumPyArrayToFeatureClass
#数组转featureclass
def InsertRow(savepath, featurename, data):
bikeid = []
lng = []
lat = []
for value in range(1,len(data)+1):
bikeid.append(float(data.head(value)['bikeid'][value-1]))
lng.append(float(data.head(value)['lng'][value-1]))
lat.append(float(data.head(value)['lat'][value-1]))
numpy_arr = np.rec.fromarrays([bikeid, lng, lat],
formats=['U8','f8','f8'],
names=["单车编号","经度","纬度"])
#NumPyArrayToFeatureClass
out_path = savepath
feat_name = featurename + ".shp"
out_feat = os.path.join(out_path, feat_name)
sr = arcpy.SpatialReference(4326)
arcpy.da.NumPyArrayToFeatureClass(numpy_arr, out_feat, ['经度', '纬度'], sr)
是不是被Arcpy所迷倒
转载自:https://blog.csdn.net/qq_24655701/article/details/84862975