python xy coordinates

914
4
04-14-2021 07:32 AM
jelliott22
New Contributor

Hi Everyone. I have a lot of xy coordinates from different excel spreadsheets. Does anyone have any sample code for python in order to plot the coordinates on the map? as oppose to me having to individually add each file, display XY data, select the proper coordinate system. Thank you

0 Kudos
4 Replies
jcarlson
MVP Notable Contributor

If you read the CSVs into a pandas DataFrame, the ArcGIS Python API has the function GeoAccessor.from_xy() that can convert your X and Y into point features that you can then plot on a map.

 

import pandas as pd
from arcgis.features import GeoAccessor

xy_df = pd.read_csv('filepath')

sdf = GeoAccessor.from_xy(xy_df, 'x-column', 'y-column', 'spatialReference')

sdf.spatial.plot()

 

If you're looking to get all the Excel sheets onto the same map, you can either repeat that process and specify an existing map widget in the plot function.

Or you could merge your Excel dataframes together, if their schemas allow it, prior to creating a spatial dataframe from them.

- Josh Carlson
Kendall County GIS
jelliott22
New Contributor

Thank you for your help. I'm relatively new to python and i'll try to figure how to download the pandas module to see if that helps. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What have you tried so far?

The code seems to have two logical parts:  1) listing/finding Excel spreadsheets, 2) converting XY coordinates in spreadsheets to point geometries on a map.

For the first part, Python's walk() function is a standard approach:

 

import os

wd =  # path to starting/working directory
for root, dirs, files in os.walk(wd):
    for f in files:
        if f.endswith((".xls", ".xlsx")):
            print(os.path.join(root, f))

 

Hopefully the names of the worksheets are standardizes; otherwise, you will have to do an extra step to list them out as well.  Once you have the workbook path and worksheet name, you can use Make XY Event Layer (Data Management)—ArcGIS Pro | Documentation

jelliott22
New Contributor

ok thank you for your help.  I'm pretty good with ArcMap but relatively new to Python. I'm just trying to figure out an easy way to upload different excel spreadsheets with latitude and longitudes and plot them on my map. There has got to be a standard solution.  I've tried looking around for the past 4 hrs and nothing seems to be simple. I'll keep trying and look around. Thanks again. 

0 Kudos