Vector format operation & conversion with arcpy

Kochizufan was baptized by ArcGIS for the first time after changing jobs because he had only used Open Source GIS. It seems to be shit expensive, but if you want to do the same processing, Arc is much faster! Well, I think it's natural because everyone would have chosen it if it was just expensive and didn't deal with it ...

So, there is a requirement to read data from a vector map format (example: Shape file), process each attribute in various ways, and spit it out to another format (example: FGDB), which is arcpy (ArcGIS python environment). ), I found out how to do it, so I will write some know-how.

Why did you have a hard time

To be honest, I had a lot of trouble finding out. For some reason, most arcpy articles, first

import arcpy

# Set the workspace environment to local file geodatabase
arcpy.env.workspace = "C:/data/base.gdb"

It says, and you can list the schemas below from here, but I misunderstood the process of specifying the DB folder in this workspace attribute as "the process of opening the DB". So, I was confused when I thought, "Do you specify the DB to open without creating an instance? What happens when you want to open multiple databases, read them, and poke them in?" Actually, the number of articles is drastically reduced, but there was also an interface that normally creates an instance of the Cursor class. Maybe I'm the only one, but some people may get stuck in the same swamp, so share the addiction in advance.

Processing sample to open and plunge

So, a sample of the code that actually opens, processes, and plunges.

arcpy sample


import arcpy
from arcpy import env
import datetime
 
#Original Shapefile
sFClass = "D:/Data/SampleShape/SampleScheme.shp"
#Read the definition object of the original file
sDesc   = arcpy.Describe(sFClass)
#Read field definition list
sFields = sDesc.fields
#Create a field name list from a field definition list
sKeys   = [field.name for field in sFields]
#Removed auto-numbered system ID field(For Shape)
sKey.remove('FID')
#Removed Shape field, which is a geometry field
#(Because the default Shape field only gives point information of representative points)
#Added field to extract geometry in WKB
sKey.remove('Shape')
sKey.append('Shape@WKB')
 
#FGDB file to write to
fFClass = "D:/Data/Sample_FGDB.gdb/SAMPLE_SCHEME"
#Read the definition object of the destination file
fDesc   = arcpy.Describe(fFClass)
#Read field definition list
fFields = fDesc.fields
#Create a field name list from a field definition list
fKeys   = [field.name for field in fFields]
#Removed auto-numbered system ID field(For FGDB)
fKey.remove('OBJECTID')
#Removed Shape field, which is a geometry field
#(Because the default Shape field can only write point information of representative points)
#Added field for writing geometry in WKB
fKey.remove('Shape')
fKey.append('Shape@WKB')
 
#Read the original file and open the cursor (search results will appear in the order of sKeys)
#In addition, arcpy.da is ArcGIS 10.It seems to be from 1 or later, so it seems that it will not work before that...
#My colleague's PC is 10.It didn't work because it was 0
sCur = arcpy.da.SearchCursor(sFClass, sKeys)
#Open Cursor by adding the destination file(Write in a list of fKeys)
fCur = arcpy.da.InsertCursor(fFClass, fKeys)
#Read line by line
for sRow in sCur:
    #Since sRow comes as a list of sKeys, zip it into a dictionary object.
    sDic = dict(zip(sKeys,sRow))
    #Create write data in dictionary (the following mapping is appropriate)
    fDic = {
        "CODE"          : "ID_" + sDic["ID"],
        "Shape@WKB"     : sDic["Shape@WKB"],
        "CUSTOMER_NAME" : sDic["FIRST_NM"] + " " + sDic["LAST_NM"],
        "Importance"    : sDic["Priority"],
        "Even_Or_Odd"   : sDic["NUMBER"] % 2
    }
    #Expand the write data dictionary to a list in order of fKeys (Note: If there is no value corresponding to all fKeys in this writing method, an error will occur)
    fRow = [fDic[key] for key in fKey]
    #Insert value into FGDB
    fCur.insertRow(fRow)

del fCur
del sCur

With this kind of feeling, you can easily create a vector map file with a different schema and format from the original vector map file with arcpy.

Yurubo

FOSS 4G version write someone

Recommended Posts

Vector format operation & conversion with arcpy
[python] vector operation
SQL format with sqlparse
Try matrix operation with NumPy
S3 operation with python boto3
Format C source with pycparser
Format json with Vim (with python)
String format with Python% operator