ArcGIS API for Python - XY to Line

626
2
Jump to solution
02-11-2022 11:42 AM
ChadPeltier
New Contributor II

Is there a function in the ArcGIS API for Python for the XY to Line tool (like arcpy's arcpy.management.XYToLine)? I haven't been able to find it in the documentation if it exists. 


0 Kudos
1 Solution

Accepted Solutions
ChadPeltier
New Contributor II

Thanks Dan. 

For anyone else looking for something similar in the future, I ended up using the GeodesicLinesToGIS module as the basis for this. Given a DataFrame called 'start_df' that contains two sets of long/lats i.e. X1, Y1, X2, Y2:

 

from shapely.geometry import Point, LineString
import pandas as pd
from arcgis.features import GeoAccessor
from geodesiclinestogis.geodesicline2gisfile import GeodesicLine2Gisfile
gtg = GeodesicLine2Gisfile()

def make_geodesic_line(row):
    long_lats = tuple(row)
    cd = gtg.gdlComp(long_lats)

    line = [Point(x) for x in cd]
    line = LineString(line)
    line = str(line)
    
    return line
    
lines = start_df.apply(make_geodesic_line, axis = 1)

df = pd.DataFrame({
    'id' : list_of_ids,
    'SHAPE' : lines
})

sedf = pd.DataFrame().spatial.from_df(df, geometry_column = 'SHAPE')

 

If you wanted, then it shouldn't be too difficult to take the calculations from the GeodesicLinesToGIS module directly instead of importing it as a dependency. 

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

No, but you can roll out your own if you want to use arcpy or even shapely


... sort of retired...
0 Kudos
ChadPeltier
New Contributor II

Thanks Dan. 

For anyone else looking for something similar in the future, I ended up using the GeodesicLinesToGIS module as the basis for this. Given a DataFrame called 'start_df' that contains two sets of long/lats i.e. X1, Y1, X2, Y2:

 

from shapely.geometry import Point, LineString
import pandas as pd
from arcgis.features import GeoAccessor
from geodesiclinestogis.geodesicline2gisfile import GeodesicLine2Gisfile
gtg = GeodesicLine2Gisfile()

def make_geodesic_line(row):
    long_lats = tuple(row)
    cd = gtg.gdlComp(long_lats)

    line = [Point(x) for x in cd]
    line = LineString(line)
    line = str(line)
    
    return line
    
lines = start_df.apply(make_geodesic_line, axis = 1)

df = pd.DataFrame({
    'id' : list_of_ids,
    'SHAPE' : lines
})

sedf = pd.DataFrame().spatial.from_df(df, geometry_column = 'SHAPE')

 

If you wanted, then it shouldn't be too difficult to take the calculations from the GeodesicLinesToGIS module directly instead of importing it as a dependency. 

0 Kudos