0

I'm looking to create a new feature class using only selected data from a source feature class. I can get this to work but the ShapeLength field is 0 when I run this script and therefore the feature class has no spatial data. All the other fields are properly filled.

I think I'm missing a step or an array or something but the data is coming right from the source feature class not a list so I don't know how to get it in.

import arcpy, os, zipfile
from arcpy import env

env.workspace = "Database Connections\\CNTM_MAC_GIS(EPRD-SHS12-LTR,62246).sde"
env.overwriteOutput=True

# Setting Sources

    #spatial reference
sr1 = "GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision"


BreadSource= "//natn4/NAT_shrdata/MAC/GIS/Data/GPS/Geodatabases/GPS_data.gdb/All_2018_GPS_data"
BreadTarget=arcpy.CreateFeatureclass_management(GDBLocation, "GPS_Routes", "POLYLINE", BreadSource, "", "",sr1 ) # set output location

dsc = arcpy.Describe(BreadSource)
fields = dsc.fields



fieldnames = [field.name for field in fields if field.name != dsc.OIDFieldName]
#fieldnames = [field.name for field in fields]

with arcpy.da.SearchCursor(BreadSource,fieldnames, "Date_dt > date "+ "'" + yyyy + '-' + smm + '-'+ sd + " 00:00:00' AND Date_dt < date "+ "'" + yyyy + '-' + emm + '-'+ ed + " 23:59:59'") as sCur: #add sql where statement to extract subset of data
    with arcpy.da.InsertCursor(BreadTarget,fieldnames) as iCur:
        for row in sCur:
            iCur.insertRow(row)

del sCur
del iCur
  • There are many potential problems with pasting one row to another without verifying the equivalence of the spatial reference. Given the with blocks, the del directives are unnecessary. – Vince Dec 27 '18 at 17:01
1

You don't need to use cursors to copy selected features:

# make a feature layer from the source data
arcpy.makeFeatureLayer_management(BreadSource, "BreadLayer")

# select features in the layer
arcpy.SelectLayerByAttribute_management("BreadLayer", "NEW_SELECTION", "Date_dt > date "+ "'" + yyyy + '-' + smm + '-'+ sd + " 00:00:00' AND Date_dt < date "+ "'" + yyyy + '-' + emm + '-'+ ed + " 23:59:59'" )

# set an output target
BreadTarget = "path\to\your\gdb\GPS_Routes"

# copy the selected features
arcpy.CopyFeatures_management(BreadLayer, BreadTarget)
  • Thank you I was over complicating due do a datebase issue where I could only select 1000 features at a time but luckily this dataset is not on that same database. Thank you! If I did need to use curcors, is there a way to do it with Polylines? – aguay091 Dec 27 '18 at 17:14
  • 1
    My guess is that much like you skip the OID, you should also skip the geometry field and instead use the "SHAPE@" magic token. – Marc Pfister Dec 27 '18 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.