Skip to main content

【ArcPy编程】获取几何图形的GeoJson与Extent

在做Webgis相关功能读加载几何图形时,需要的是几何图形的GeoJson格式的数据;Arcpy中的Geometry、FeatureSet、RecordSet对象 提供了JSON属性可以直接获取到几何图形的GeoJson格式的数据;

下面举例通过Geometry对象获取到几何图形的json与Extent属性值;具体代码与详细的注释如下:

方式一

#coding=utf-8
import arcpy
from arcpy import  env
import  os,sys,json

reload(sys)
sys.setdefaultencoding('utf8')

env.workspace = os.getcwd()

shpFile='/shps/C.shp'   #图层路径

SearchCursor=arcpy.SearchCursor(shpFile)
for row in SearchCursor:
   Geo=row.shape
   GeoJsonStr=Geo.JSON   #获得图形的json格式数据
   print GeoJsonStr
   print type(GeoJsonStr)
   # 把字符串格式的Geojson转换为字典格式,方便获取里面的信息
   GeoJon=json.loads(GeoJsonStr)
   print "GeoJon Type:",type(GeoJon)    #通过type函数查看变量的数据类型
   print GeoJon 
     #输出信息
   print 'rings:',GeoJon['rings']    #获取到Geojson里的rings信息
   print "wkt:",GeoJon['spatialReference']['wkt']   #获取参考系信息

   #获取图形的四至信息(最大最小的XY)并输出
   extent=Geo.extent
   print "XMax:",extent.XMax
   print "YMax:", extent.YMax
   print "XMin:", extent.XMin
   print "YMin:", extent.YMin

输出结果

{"rings":[[[444898.63150000013,4414552.9708999991],[444984.29050000012,4414555.2860000003],[444983.62899999972,4414566.8615000006],[444982.96760000009,4414585.7130999994],[444983.62899999972,4414603.9033000004],[444984.95199999958,4414622.4242000002],[445038.1995000001,4414623.7470999993],[445042.49890000001,4414501.0462999996],[444957.17059999984,4414499.7234000005],[444956.17850000039,4414541.0646000002],[444898.96219999995,4414541.7260999996],[444898.63150000013,4414552.9708999991]]],"spatialReference":{"wkt":"PROJCS[\"CGCS_2000_Transverse_Mercator\",GEOGCS[\"GCS_CGCS_2000\",DATUM[\"D_CGCS_2000\",SPHEROID[\"CGCS_2000\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"false_easting\",500000.0],PARAMETER[\"false_northing\",0.0],PARAMETER[\"central_meridian\",117.0],PARAMETER[\"scale_factor\",1.0],PARAMETER[\"latitude_of_origin\",0.0],UNIT[\"Meter\",1.0]]"}}
<type 'unicode'>
GeoJon Type: <type 'dict'>
{u'rings': [[[444898.63150000013, 4414552.970899999], [444984.2905000001, 4414555.286], [444983.6289999997, 4414566.861500001], [444982.9676000001, 4414585.713099999], [444983.6289999997, 4414603.9033], [444984.9519999996, 4414622.4242], [445038.1995000001, 4414623.747099999], [445042.4989, 4414501.0463], [444957.17059999984, 4414499.7234000005], [444956.1785000004, 4414541.0646], [444898.96219999995, 4414541.7261], [444898.63150000013, 4414552.970899999]]], u'spatialReference': {u'wkt': u'PROJCS["CGCS_2000_Transverse_Mercator",GEOGCS["GCS_CGCS_2000",DATUM["D_CGCS_2000",SPHEROID["CGCS_2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["false_easting",500000.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",117.0],PARAMETER["scale_factor",1.0],PARAMETER["latitude_of_origin",0.0],UNIT["Meter",1.0]]'}}
rings:[[[444898.63150000013, 4414552.970899999], [444984.2905000001, 4414555.286], [444983.6289999997, 4414566.861500001], [444982.9676000001, 4414585.713099999], [444983.6289999997, 4414603.9033], [444984.9519999996, 4414622.4242], [445038.1995000001, 4414623.747099999], [445042.4989, 4414501.0463], [444957.17059999984, 4414499.7234000005], [444956.1785000004, 4414541.0646], [444898.96219999995, 4414541.7261], [444898.63150000013, 4414552.970899999]]]
wkt: PROJCS["CGCS_2000_Transverse_Mercator",GEOGCS["GCS_CGCS_2000",DATUM["D_CGCS_2000",SPHEROID["CGCS_2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["false_easting",500000.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",117.0],PARAMETER["scale_factor",1.0],PARAMETER["latitude_of_origin",0.0],UNIT["Meter",1.0]]
XMax: 445042.4989
YMax: 4414623.7471
XMin: 444898.6315
YMin: 4414499.7234

ArcGIS10.1后的版本添加了da操作数据模块,引入了令牌的功能,可以根据自己需求直接获取对应的信息,令牌相关知识可以参考官网帮助文档;例如通过

SHAPE@JSON令牌获得几何图形的json信息。下面的代码使用with的好处是使用完的游标后会自动释放资源,避免资源被占用。

方式二

with arcpy.da.SearchCursor(shpFile,['SHAPE@JSON','SHAPE@WKT','SHAPE@AREA']) as SearchCursor:
    for row in SearchCursor:
        GeoJsonStr=row[0]
        WKTStr = row[1]
        AREA = row[2]
        print "GeoJsonStr",GeoJsonStr
        GeoJson = json.loads(GeoJsonStr)
        print 'rings:', GeoJson['rings']  # 获取到Geojson里的rings信息
        print "wkt:", GeoJson['spatialReference']['wkt']  # 获取参考系信息 
        print "WKTStr", WKTStr
        print "AREA:", AREA

输出结果

GeoJsonStr {"rings":[[[444898.63150000013,4414552.9708999991],[444984.29050000012,4414555.2860000003],[444983.62899999972,4414566.8615000006],[444982.96760000009,4414585.7130999994],[444983.62899999972,4414603.9033000004],[444984.95199999958,4414622.4242000002],[445038.1995000001,4414623.7470999993],[445042.49890000001,4414501.0462999996],[444957.17059999984,4414499.7234000005],[444956.17850000039,4414541.0646000002],[444898.96219999995,4414541.7260999996],[444898.63150000013,4414552.9708999991]]],"spatialReference":{"wkt":"PROJCS[\"CGCS_2000_Transverse_Mercator\",GEOGCS[\"GCS_CGCS_2000\",DATUM[\"D_CGCS_2000\",SPHEROID[\"CGCS_2000\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"false_easting\",500000.0],PARAMETER[\"false_northing\",0.0],PARAMETER[\"central_meridian\",117.0],PARAMETER[\"scale_factor\",1.0],PARAMETER[\"latitude_of_origin\",0.0],UNIT[\"Meter\",1.0]]"}}
rings:[[[444898.63150000013, 4414552.970899999], [444984.2905000001, 4414555.286], [444983.6289999997, 4414566.861500001], [444982.9676000001, 4414585.713099999], [444983.6289999997, 4414603.9033], [444984.9519999996, 4414622.4242], [445038.1995000001, 4414623.747099999], [445042.4989, 4414501.0463], [444957.17059999984, 4414499.7234000005], [444956.1785000004, 4414541.0646], [444898.96219999995, 4414541.7261], [444898.63150000013, 4414552.970899999]]]
wkt: PROJCS["CGCS_2000_Transverse_Mercator",GEOGCS["GCS_CGCS_2000",DATUM["D_CGCS_2000",SPHEROID["CGCS_2000",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["false_easting",500000.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",117.0],PARAMETER["scale_factor",1.0],PARAMETER["latitude_of_origin",0.0],UNIT["Meter",1.0]]
WKTStr MULTIPOLYGON (((444898.96219999995 4414541.7260999996, 444956.17850000039 4414541.0646000002, 444957.17059999984 4414499.7234000005, 445042.49890000001 4414501.0462999996, 445038.1995000001 4414623.7470999993, 444984.95199999958 4414622.4242000002, 444983.62899999972 4414603.9033000004, 444982.96760000009 4414585.7130999994, 444983.62899999972 4414566.8615000006, 444984.29050000012 4414555.2860000003, 444898.63150000013 4414552.9708999991, 444898.96219999995 4414541.7260999996)))
AREA: 9143.20710096

发表回复