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?
Is there a way to do this with a df that has more than 1000 rows? This script throws an error for that case..
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.
rsingh-esristaff
How does this work it my data is in a different coordinate system (i.e. not WGS 1984)?
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
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:
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?
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
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.