【2】Arcpy生成shape文件

171 阅读2分钟
# coding=utf8
import os
import arcpy
import json

geojson = {"type": "Polygon", "coordinates": [
    [[120.81878662109375, 44.54437255859375], [120.74359130859375, 44.54150390625], [120.769775390625, 44.599365234375],
     [120.8216552734375, 44.64581298828125], [120.87115478515625, 44.5936279296875],
     [120.81878662109375, 44.54437255859375]]]}

# geom = arcpy.AsShape(geojson)

sp = arcpy.SpatialReference(4326)
path = r'd:/upload/test'
fc = arcpy.CreateFeatureclass_management(path, "1.shp", "POLYGON", "", "", "", sp)
arcpy.AddField_management(fc, "Polygon", "TEXT", 50)
cursor = arcpy.InsertCursor(fc)

row = cursor.newRow()
row.shape = arcpy.AsShape(geojson)
row.Polygon = "Polygon"
cursor.insertRow(row)
print "面要素创建完成!"

根据geojson生成一个shape文件

# coding=utf8
import os
import arcpy
import json

# json_str='{"type":"FeatureCollection","name":"1","features":[{"type":"Feature","properties":{"uuid":"1","adderss":"白云区小坪东路20号"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},{"type":"Feature","properties":{"uuid":"2","adderss":"锦江区人民南路四段217号"},"geometry":{"type":"Point","coordinates":[2.0,0.0]}},{"type":"Feature","properties":{"uuid":"3","adderss":"福田区景田东一街486号"},"geometry":{"type":"Point","coordinates":[3.0,0.0]}},{"type":"Feature","properties":{"uuid":"4","adderss":"白云区机场路棠苑街五巷615号"},"geometry":{"type":"Point","coordinates":[4.0,0.0]}},{"type":"Feature","properties":{"uuid":"5","adderss":"坑美十五巷773号"},"geometry":{"type":"Point","coordinates":[5.0,5.0]}},{"type":"Feature","properties":{"uuid":"6","adderss":"天河区天河路252号"},"geometry":{"type":"Point","coordinates":[0.0,1.0]}},{"type":"Feature","properties":{"uuid":"7","adderss":"西城区复兴门内大街31号"},"geometry":{"type":"Point","coordinates":[0.0,2.0]}},{"type":"Feature","properties":{"uuid":"8","adderss":"越秀区中山二路96号"},"geometry":{"type":"Point","coordinates":[0.0,3.0]}},{"type":"Feature","properties":{"uuid":"9","adderss":"成华区二仙桥东三路712号"},"geometry":{"type":"Point","coordinates":[0.0,4.0]}},{"type":"Feature","properties":{"uuid":"10","adderss":"龙岗区学园一巷386号"},"geometry":{"type":"Point","coordinates":[0.0,5.0]}},{"type":"Feature","properties":{"uuid":"11","adderss":"龙岗区学园一巷914号"},"geometry":{"type":"Point","coordinates":[5.0,0.0]}}]}'

data = {"type": "FeatureCollection", "name": "1", "features": [
    {"type": "Feature", "properties": {"uuid": "1", "adderss": "白云区小坪东路20号"},
     "geometry": {"type": "Point", "coordinates": [0, 0]}},
    {"type": "Feature", "properties": {"uuid": "2", "adderss": "锦江区人民南路四段217号"},
     "geometry": {"type": "Point", "coordinates": [2, 0]}},
    {"type": "Feature", "properties": {"uuid": "3", "adderss": "福田区景田东一街486号"},
     "geometry": {"type": "Point", "coordinates": [3, 0]}},
    {"type": "Feature", "properties": {"uuid": "4", "adderss": "白云区机场路棠苑街五巷615号"},
     "geometry": {"type": "Point", "coordinates": [4, 0]}},
    {"type": "Feature", "properties": {"uuid": "5", "adderss": "坑美十五巷773号"},
     "geometry": {"type": "Point", "coordinates": [5, 5]}},
    {"type": "Feature", "properties": {"uuid": "6", "adderss": "天河区天河路252号"},
     "geometry": {"type": "Point", "coordinates": [0, 1]}},
    {"type": "Feature", "properties": {"uuid": "7", "adderss": "西城区复兴门内大街31号"},
     "geometry": {"type": "Point", "coordinates": [0, 2]}},
    {"type": "Feature", "properties": {"uuid": "8", "adderss": "越秀区中山二路96号"},
     "geometry": {"type": "Point", "coordinates": [0, 3]}},
    {"type": "Feature", "properties": {"uuid": "9", "adderss": "成华区二仙桥东三路712号"},
     "geometry": {"type": "Point", "coordinates": [0, 4]}},
    {"type": "Feature", "properties": {"uuid": "10", "adderss": "龙岗区学园一巷386号"},
     "geometry": {"type": "Point", "coordinates": [0, 5]}},
    {"type": "Feature", "properties": {"uuid": "11", "adderss": "龙岗区学园一巷914号"},
     "geometry": {"type": "Point", "coordinates": [5, 0]}}]}

fildNames = set()
for key, value in data["features"][0]["properties"].items():
    fildNames.add(key)

print fildNames

sp = arcpy.SpatialReference(4326)
path = r'd:/upload/test'
fc = arcpy.CreateFeatureclass_management(path, "1.shp","Point","","","", sp)
for fildName in fildNames:
    arcpy.AddField_management(fc, fildName, "TEXT", 255)

cursor = arcpy.InsertCursor(fc)

for feature in iter(data["features"]):
    row = cursor.newRow()
    row.shape = arcpy.AsShape(feature["geometry"])
    for key, value in feature["properties"].items():
        row.setValue(key,value)

    cursor.insertRow(row)

print "面要素创建完成!"

生成属性和字段