0

I’m developing a script tool using ArcPy. I am wondering if there any way to store layer contents in my script in an encoded way and programmatically create this layer instead of referencing external shp/geodatabase feature class?

I need this to distribute only the .tbx file without any dependent .shp files. The nature of layer – rectangular grid of polygons with attributes, inside a relatively complex boundary(Iowa state border), so it difficult to create this grid programmatically without at least border geometry. Now I’m using code like:

foo="C:\foo\bar.shp"
arcpy.MakeFeatureLayer_management(foo, "bar")

to add my layer. Would it be possible to somehow encode this feature layer geometry and attributes, store it as variable/list (like WKT, GeoJSON, KML) and use something like that

jsonEncodedString=" {\"type\": \"FeatureCollection\",\"features\": [{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Polygon\",\"coordinates\": [[[87.890625,67.60922060496382],[78.046875,64.47279382008166],[82.265625,63.54855223203644],[88.9453125,64.16810689799152],[90.3515625,65.80277639340238],[87.890625,67.60922060496382]]]  }},{\"type\": \"Feature\",\"properties\": {},\"geometry\": {\"type\": \"Polygon\",\"coordinates\":[[63.6328125,63.860035895395306],[55.54687499999999,64.16810689799152],[54.4921875,63.23362741232569],[59.765625,60.58696734225869],[63.6328125,63.860035895395306]]]}}]} "
some_function(jsonEncodedString, "layer_name")

to create a feature layer with attributes instead of referencing an external shapefile/feature class?

2
  • For a start it should be foo="C:\\foo\\bar.shp" or foo=r"C:\foo\bar.shp" as the backslash is the escape char. No, you can't store a layer in a tool but you can reference the executing code path (os.path.dirname(sys.argv[0])) which you can combine with a layer file barLayer = arcpy.mapping.Layer(os.path.join(os.path.dirname(sys.argv[0]),'bar.lyr')) to load the layer from a file then barLayer.replaceDataSource(os.path.dirname(sys.argv[0]),'SHAPEFILE_WORKSPACE') to ensure the layer points to the correct shapefile. When you distribute the toolbox include the script, layer file and shapefile. Nov 8 '18 at 4:01
  • But you can include a geometry, as tuples of (x,y{,z}), then create a feature class in in_memory and insert the geometry using an insert cursor if that is what you need.. see the example (all the way down the bottom) resources.arcgis.com/en/help/main/10.2/index.html#/Polygon/… you can write a script to export the coordinates as text from your bar.shp, copy and paste into your script. Nov 8 '18 at 4:08
0

Thank you for the comments. So the easiest way to solve my question was to convert geometries to WKT format and wrap it to the tuple, along with feature attribute. Then I used insert cursor, as suggested in the document, advised by Michael Stimson. So my final code looks like below:

#setting a tuple with WKT feature representation and attribute value
row_values = [('POLYGON ((202000 4732000,202000 4734000,204000 4734000,204000 4732000,202000 4732000))', '02024732'),
('POLYGON ((202000 4734000,202000 4736000,204000 4736000,204000 4734000,202000 4734000))', '02024734'),
('POLYGON ((202000 4736000,202000 4738000,204000 4738000,204000 4736000,202000 4736000))', '02024736')]

#setting workspace to use in-memory workspace
arcpy.env.workspace='in_memory'

#creating an empty feature class in the in-memory workspace
arcpy.CreateFeatureclass_management('in_memory', 'tilegrid', 'POLYGON', '', '', '', arcpy.SpatialReference(26915))

#adding a field to store attribute values
arcpy.AddField_management ('tilegrid', 'TILE', 'TEXT')

# Open an InsertCursor
cursor = arcpy.da.InsertCursor('tilegrid',['SHAPE@WKT', 'TILE'])

# Insert new rows that include the geometry, converted from WKT and attribute values
for row in row_values:
    cursor.insertRow(row)

# Delete cursor object
del cursor

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.