0

I want to insert rows which the field CNTY_NM meets the requirement. The code is OK for running, but the feature class only has the records but no geometry. What is wrong with my code?

import arcpy
import os

int_buffer_TXcounties = r'C:\Users\TL\Desktop\1216\buffer\int.shp'
inputfeatureclass = r'C:\Users\TL\Desktop\1212\TXCounty\TXCounty.shp'

#Create an empty featureclass by using inputfeatureclass as template
arcpy.CreateFeatureclass_management(out_path="C:/Users/TL/Desktop/1216/buffer", out_name="TC1", geometry_type="POLYGON", template="C:/Users/TL/Desktop/1216/buffer/TX_counties.shp", has_m="DISABLED", has_z="DISABLED", spatial_reference="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", config_keyword="", spatial_grid_1="0", spatial_grid_2="0", spatial_grid_3="0")
insertfeatureclass = r'C:\Users\TL\Desktop\1216\buffer\TC1.shp'

#get the counties names
counties_name = []
fields = ["CNTY_NM","CNTY_FIPS"]
with arcpy.da.SearchCursor(int_buffer_TXcounties, fields) as cursor:
    for row in cursor:
        print("{0}, {1}".format(row[0], row[1]))
        counties_name.append(row[0])

#Insert counties
for county in counties_name:
    print county
    expression = """"CNTY_NM" = '{0}'""".format(county)
    print expression
    with arcpy.da.InsertCursor(insertfeatureclass, '*') as i_cursor:
        with arcpy.da.SearchCursor(inputfeatureclass, '*', where_clause=expression) as s_cursor:
            for s_row in s_cursor:
                i_cursor.insertRow(s_row)

The following picture shows the records in the feature class. But when I used Zoom to Layer, I can't have any geometry.

I am using ArcGIS 10.3.1 Advanced License. enter image description here

1
  • Why are you not using arcpy.CopyFeatures_management using a selection from the input fc? Dec 18 '16 at 7:35
1

The 1st thing I see is you don't have a spatial index grid calculated in your CreateFeatureclass command....leave out those optional parameters to allow ArcGIS calculate a default index, so something like:

spatial_reference="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"
arcpy.CreateFeatureclass_management(out_path="C:/Users/TL/Desktop/1216/buffer", out_name="TC1", geometry_type="POLYGON", template="C:/Users/TL/Desktop/1216/buffer/TX_counties.shp", has_m="DISABLED", has_z="DISABLED", spatial_reference)

Edit: By the way, if you don't want to modify any of your existing code, you should be able to run at the end of your script the command to add the spatial index (which can also update it if one is set inappropriately):

arcpy.AddSpatialIndex_management (insertfeatureclass)

...or, if you just want to do that part via Catalog, then see: http://desktop.arcgis.com/en/arcmap/10.3/manage-data/shapefiles/modifying-indexes-in-shapefiles-by-updating-a-spatial-index.htm

1
  • Your method does work.
    – Tianxin Li
    Dec 18 '16 at 22:44

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.