import Pandas DataFrame with Polyline field using Python API

2538
5
02-10-2017 09:06 AM
Jay_Gregory
Occasional Contributor III

Been playing around with the Python API.  I'm using a Pandas dataframe, to which I've added a column that has a Polyline (arcgis.geometry.Polyline class) as it's attribute.  I would like to add this dataframe to a map, but the import_data method only seems to work on dataframes with x,y or lat,long column names, and I couldn't find a way to specify the Shape field when invoking the method.  Is this possible?

My code, in general, looks like:

fc_lines =  gis.content.import_data(df_lines)

map1 = gis.map()
map1.add_layer(fc_lines)

df_lines is a dataframe with a column, named "Lines" that contains an arcgis.geometry.Polyline object.  At least I think it contains an object - when I print out the dataframe, that column has something like

{"paths": [[32.08138, 40.17111], [35.182777, 39.4016666]], "spatialReference": {"wkid": 4326}}

which is the string representation of the object. But if it's actually stored as a string, that's a different matter (and maybe why it won't work).  

Is there a way to do what I want using the Python API, or should I somehow export the dataframe to a feature class, if that's even possible.  

0 Kudos
5 Replies
JillKelly
New Contributor III

If you actually have a polyline geometry, try following it with

lines = arcgis.features.Feature(fc_lines)
lines1 = arcgis.features.FeatureSet([lines])

and then

map1.draw(lines1) instead of add_layer.

If not, to get from the text string to a geometry, you'd need

fc_lines = arcgis.geometry.Polyline(fc_string_thing)

before all that. 

Hope this works; I got it working for pandas-dataframe-to-polygon. 

0 Kudos
Jay_Gregory
Occasional Contributor III

I would love to see a code example if you have one.  I could not get this to work.  I am able to create a FeatureSet made up of Features (the feature is of type "Polyline"), but I can't pass the FeatureSet into draw (I believe the function only takes one item to draw, not a list).  And even if I pass in a single Feature, the map refreshes, but I don't see any graphics...

#lines1 = features.FeatureSet(features = df_lines['Lines'])

map1 = gis.map()
symbol = {
   "type": "esriSLS",
   "style": "esriSLSSolid",
   "color": [115,76,0,255],
   "width": 3
}

for item in df_lines['Lines']:
   print("drawing " + str(item.geometry))
   map1.draw(shape = item.geometry, symbol=symbol)
map1

At the very least, I can't even get this simple example to work: can you?

my_line = arcgis.geometry.Polyline({"paths": [[38.8,-78.8],[34.2,-117]]})
my_line_feature = arcgis.features.Feature(my_line, attributes=None)

map1.draw(my_line, symbol=symbol)

OR

map1.draw(my_line_feature, symbol=symbol)

0 Kudos
Jay_Gregory
Occasional Contributor III

Even though I mixed up x and y, it still won't work

0 Kudos
JillKelly
New Contributor III

Much stripped down, this works:

my_line_geom = {'paths' : [[[-78.8,38.8],[-78.8,38.2]]]}
my_line = arcgis.geometry.Polyline(my_line_geom)
my_line_feature = arcgis.features.Feature(my_line)
my_line_feature_set = arcgis.features.FeatureSet([my_line_feature])
map1 = gis.map('Orkney Springs')
map1.draw(my_line_feature_set)
map1

I have not been able to draw both features of a two-feature feature set.  Only the second one shows up.  Nor have I had any success setting the symbology.

0 Kudos
Jay_Gregory
Occasional Contributor III

Thanks - so it seems like Esri still has some work to do on this API....

0 Kudos