1

polygonzo json ポリゴン ( http://code.google.com/p/polygonzo/ ) を作成するために、ArcGIS arcpy を使用して python スクリプトをまとめました。これが私のpythonスクリプトです...

 import os, string, arcpy
 arcpy.env.overwriteOutput = True


 layer = "C:\\Other\\Shapefiles\\Geo500K_JSON\\GEOLOGY_500K_Project.shp"

 output = "C:\\Other\\Shapefiles\\Geo500K_JSON\\"

 outfile = output + "Geo500K.json"
 jsonFile = open(outfile,'w')
 jsonFile.write('var geo = {\n')
 jsonFile.write('\t"type": "FeatureCollection",\n')
 jsonFile.write('\t"features": [\n')

 idfield = "ORIG_LABEL"
 shape_field = arcpy.Describe(layer).shapeFieldName

 rows = arcpy.SearchCursor(layer,"","","",idfield + " A")
 row = rows.next()

 while row:

     geostring = '' #for each lat/lng pt
     geolist = [] # array for storing individual geostrings
     ringList = [] #array for storing geolist array with geostrings separated by commas
     partList = [] #array for storing partlist, final array used
     shapeString = ''

     jsonFile.write('\t\t{"type": "Feature", ')

     extent = row.Shape.extent
     ne = str(extent.XMax) + ',' + str(extent.YMax)
     sw = str(extent.XMin) + ',' + str(extent.YMin)
     jsonFile.write('"bbox": [' + sw + ', ' + ne + '],')

     jsonFile.write('"properties":{')

     geoLabel = str(row.getValue(idfield))
     jsonFile.write('"label": "' + geoLabel + '", ')    

     geoName = str(row.getValue("FM_NAME"))
     jsonFile.write('"name": "' + geoName + '", ')

     lithType = str(row.getValue("LithType"))
     jsonFile.write('"lithType": "' + lithType + '", ')

     rank = str(row.getValue("Rank"))
     jsonFile.write('"rank": "' + rank + '", ')

     lithName = str(row.getValue("LithName"))
     jsonFile.write('"lithName": "' + lithName + '", ')

     ageType = str(row.getValue("AgeType"))
     jsonFile.write('"ageType": "' + ageType + '", ')

     minAge = str(row.getValue("MinAge"))
     jsonFile.write('"minAge": "' + minAge + '", ')

     maxAge = str(row.getValue("MaxAge"))
     jsonFile.write('"maxAge": "' + maxAge + '", ')

     part = row.getValue(shape_field).centroid
     jsonFile.write('"center":[' + str(part.X) + ',' + str(part.Y) + '],')

     jsonFile.write('"centroid":[' + str(part.X) + ',' + str(part.Y) + ']},')


     jsonFile.write('"geometry":{"type":"MultiPolygon","coordinates":[[[')

     feat = row.shape
     for p in range(feat.partCount):
         pInt = p
         part = feat.getPart(p)
         pt = part.next()
         while pt:
             lat = str(round(pt.Y,6))
             lon = str(round(pt.X,6))

             geostring = '[' + lon + ',' + lat + ']'
             geolist.append(geostring)

             pt = part.next()

             #if now following point go to the next part which should be an interior ring.     
             if not pt:
                 ringList.append(',' .join(geolist))
                 geostring = ''
                 geolist = []
                 pt = part.next()
                 if pt:
                     print 'Interior Ring: ' + geoLabel

         partList.append(',' .join(ringList))
         ringList = []

     shapeString = ']], [[' .join(partList)
     jsonFile.write(shapeString)
     jsonFile.write(']]]}},\n')
     row = rows.next()

 #jsonFile.seek(-1, os.SEEK_END)
 #jsonFile.truncate()
 jsonFile.write('\t]\n')
 jsonFile.write('}')
 jsonFile.close()
 del row, rows

スクリプトが内部リングに遭遇すると、警告のみを出力します。それらを処理する方法がわかりません。残念ながら、私が扱っているポリゴンの多くには内部リングがあります。内部リングを持つ 1 つのポリゴンを使用して、テスト マップをまとめました。これがその外観です... http://www.geology.ar.gov/test/test-polygonzo.html

Polygonzo は内部リングを処理できますか?

更新: マイケル ギアリーさん、ご回答ありがとうございます。ただし、json モジュールを使用して python スクリプトを動作させることができませんでした。いくつかのバグがあり、上で編集しましたが、空白のドキュメントが出力されます。努力が足りなかったのかもしれません。内部リングを持つマルチポリオンがjson形式でどのように見えるかの例を確認した後、pythonスクリプトの作業に戻りました(はい、jsonモジュールを使用せずにjsonを有効にするのは少し困難でした)。コメントを追加したので、時間があれば、json モジュールを使用してスクリプトを機能させることができます。実際の例を見てみたいと思います。これが私の最終的なpythonスクリプトです....

import os, string, arcpy
arcpy.env.overwriteOutput = True


layer = "C:\\Other\\Shapefiles\\Geo500K_JSON\\GEOLOGY_500K_kn.shp"

output = "C:\\Other\\Shapefiles\\Geo500K_JSON\\"

outfile = output + "Geo500K_knTest.json"
jsonFile = open(outfile,'w')
jsonFile.write('var geo = {\n')
jsonFile.write('\t"type": "FeatureCollection",\n')
jsonFile.write('\t"features": [\n')

idfield = "ORIG_LABEL"
shape_field = arcpy.Describe(layer).shapeFieldName

rows = arcpy.SearchCursor(layer,"","","",idfield + " A")
row = rows.next()
#loop through the attribute table
while row:    

    jsonFile.write('\t\t{"type": "Feature", \n')

    extent = row.Shape.extent
    ne = str(extent.XMax) + ',' + str(extent.YMax)
    sw = str(extent.XMin) + ',' + str(extent.YMin)
    jsonFile.write('\t\t"bbox": [' + sw + ', ' + ne + '],\n')

    jsonFile.write('\t\t"properties":{\n')

    geoLabel = str(row.getValue(idfield))
    jsonFile.write('\t\t\t"label": "' + geoLabel + '", \n')    

    geoName = str(row.getValue("FM_NAME"))
    jsonFile.write('\t\t\t"name": "' + geoName + '", \n')

    lithType = str(row.getValue("LithType"))
    jsonFile.write('\t\t\t"lithType": "' + lithType + '", \n')

    rank = str(row.getValue("Rank"))
    jsonFile.write('\t\t\t"rank": "' + rank + '", \n')

    lithName = str(row.getValue("LithName"))
    jsonFile.write('\t\t\t"lithName": "' + lithName + '", \n')

    ageType = str(row.getValue("AgeType"))
    jsonFile.write('\t\t\t"ageType": "' + ageType + '", \n')

    minAge = str(row.getValue("MinAge"))
    jsonFile.write('\t\t\t"minAge": "' + minAge + '", \n')

    maxAge = str(row.getValue("MaxAge"))
    jsonFile.write('\t\t\t"maxAge": "' + maxAge + '", \n')

    centroid = row.getValue(shape_field).centroid
    jsonFile.write('\t\t\t"center":[' + str(centroid.X) + ',' + str(centroid.Y) + '], \n')
    jsonFile.write('\t\t\t"centroid":[' + str(centroid.X) + ',' + str(centroid.Y) + '] \n')

    jsonFile.write('\t\t\t}, \n') #end of properties

    jsonFile.write('\t\t"geometry":{\n\t\t\t"type":"MultiPolygon",\n\t\t\t"coordinates":[\n')

    feat = row.shape #get the shape/geography of the row in the attribute table
    partnum = 1

    #loop through the parts of the polygon (some may have more that one part)
    for p in range(feat.partCount):
        jsonFile.write('\t\t\t\t[\n\t\t\t\t\t[\n')
        jsonFile.write('\t\t\t\t\t\t//Part ' + str(partnum) + '\n')
        jsonFile.write('\t\t\t\t\t\t//Outer ring of Part ' + str(partnum) + '\n')

        part = feat.getPart(p) #return an array of point objects for particular part

        pt = part.next() #return specific pt object of array
        innerRingNum = 1

        #loop through each pt object/vertex of part
        while pt:
            lat = round(pt.Y,7) #get latitude of pt object and round to 7 decimal places
            lon = round(pt.X,7) #get longitude of pt object and round to 7 decimal places

            jsonFile.write('\t\t\t\t\t\t[' + str(lon) + ',' + str(lat) + '],\n') #assemble [lon,lat]

            pt = part.next() #go to next pt object to continue loop

            #if no following point go to the next part which should be an interior ring.
            if not pt:
                #we've got an interior ring so let's loop through the vertices of the ring
                pt = part.next()

                if pt:
                    jsonFile.seek(-3, os.SEEK_END)
                    jsonFile.truncate() #remove trailing comma
                    jsonFile.write('\n\t\t\t\t\t],\n')
                    jsonFile.write('\t\t\t\t\t[\n')
                    jsonFile.write('\t\t\t\t\t\t//Inner ring ' + str(innerRingNum) + ' of Part ' + str(partnum) + '\n')
                    print 'Interior Ring: ' + geoLabel
                    innerRingNum += 1



        partnum += 1
        jsonFile.seek(-3, os.SEEK_END)
        jsonFile.truncate() #remove trailing comma
        jsonFile.write('\n\t\t\t\t\t]\n\t\t\t\t],\n')

    jsonFile.seek(-3, os.SEEK_END)
    jsonFile.truncate() #remove trailing comma
    jsonFile.write('\n\t\t\t]\n\t\t\t}\n\t\t},\n')
    row = rows.next()

jsonFile.seek(-3, os.SEEK_END)
jsonFile.truncate() #remove trailing comma
jsonFile.write('\n\t]\n')
jsonFile.write('}')
jsonFile.close()
del row, rows

また、polygonzo と、それを他のユーザーと共有する意欲に感銘を受けていることも付け加えさせてください。ただし、提供する javascript と python では、すべてをより迅速に理解するために、より多くのコメントを使用できます。

4

1 に答える 1