Is there a way to add x/y coordinate columns in a pandas dataframe to a map using the arcgis python api in jupyter notebook?
Yes, an example is below:
locations = [] path = r'C:\xc\Presentations\GeoPython\Watson\insulators' # find locations of broken insulators for file in listdir(path): filepath = path + '\\' + file if is_broken(filepath): locations.append(get_location(filepath)) # import into ArcGIS as a layer df = pd.DataFrame.from_records(locations) df.columns = ['x', 'y'] broken_insulators = gis.content.import_data(df) m.add_layer(broken_insulators)
I have defined get_locations to return longitude, latitude:
def get_location(filename): #... return lon, lat
Awesome! My code looks like this:
gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> data<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Longitude_X'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Latitude_Y'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> layer <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>import_data<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">)</SPAN> map <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>map<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'UK'</SPAN><SPAN class="punctuation token">)</SPAN> map<SPAN class="punctuation token">.</SPAN>add_layer<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'layer'</SPAN><SPAN class="punctuation token">)</SPAN> map<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
data is a pandas dataframe
<FeatureCollection>
The layer is a feature collection, but the lat long data doesn't show on the map. Possible reasons why?
Try setting the columns of your dataframe to x and y. The following works:
from arcgis.gis import * import pandas as pd gis = GIS() m = gis.map('UK') locations = [(52.47867, -1.90848), (51.50642, -0.12721), (53.79480, -1.54653), (53.47959, -2.24874)] df = pd.DataFrame.from_records(locations) df.columns = ['y', 'x'] cities = gis.content.import_data(df) m.add_layer(cities) m I get the following output:
Hi David,
I also had this problem in another topic on this group. The solution was the way Rohid said, but with coordinates in DD format. I had the list of coordinates in DMS format and it wasn`t working for me.
See here: https://community.esri.com/thread/198787-display-array-of-multiple-coordinates-xy-in-the-map-object
Thanks,
Ionut
rsingh-esristaff
How does this work it my data is in a different coordinate system (i.e. not WGS 1984)?
The layer/FeatureCollection to be added should have a correct spatial reference set in that case. I don't have the code offhand to do this though. Another way would be to use arcgis.geometry.project to project the coordinates to WGS1984.
Is there a way to do this with a df that has more than 1000 rows? This script throws an error for that case..
If there are more than 1000 rows, you should convert it to a SpatialDataFrame. You can do it like this:
df['SHAPE'] = df.apply(lambda row : arcgis.geometry.Geometry({'x': row['x'], 'y': row['y']}), axis=1 )
sdf = SpatialDataFrame(df)
layer = gis.content.import_data(sdf, title='My Data')
m.add_layer(layer) # add to map
find_nearest(layer, ...) # use for analysis
Thanks Rohit!The exact same code snippet is giving me an error -
RuntimeError: Service name 'a' already exists for 'bkrWlSKcjUDFDtgw'
I don't know how to debug it since I don't have services with that name/id..
There's a bug in import_data that creates a service by the name 'a' the first time it runs... the fix will be in the next update, but as a workaround you should be able to search for that service and delete it each time you're calling import_data with a spatial dataframe.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.