Select to view content in your preferred language

find closest xy on a polyline from a point

212
10
Jump to solution
Wednesday
StuartMoore
Frequent Contributor

so this sounds simple and if i use Near in Pro it is but i want to create a AGOL python notebook to run once a day to loop over any point features created that day and get the closest XY to a feature service of polylines (50k features)

i tried following the example here Part 3 - Spatial operations on geometries | ArcGIS API for Python

but even if i just manually populate the geomenty json for both the point and the polyline it returns None

StuartMoore_0-1736345673403.png

 

1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Are you using a standard Notebook?  I think you'd require an Advanced Notebook type to then be able to import the required ArcPy module.

access_res.type is probably returning none 

View solution in original post

10 Replies
DavidPike
MVP Frequent Contributor

Are you using a standard Notebook?  I think you'd require an Advanced Notebook type to then be able to import the required ArcPy module.

access_res.type is probably returning none 

StuartMoore
Frequent Contributor

thanks @DavidPike that was the issue

it looks like its returning the distance now but it looks like its in radians?

StuartMoore_0-1736415463328.png

 



0 Kudos
StuartMoore
Frequent Contributor

it also looks like its only getting the closest vertex rather than the closest point on the line unfortunately

0 Kudos
DavidPike
MVP Frequent Contributor

Are you able to share the full code and possibly the mapped polyline and point inputs and point result?  Values look like a spatial reference issue, possibly. I don't think there's any angles calculated so likely not radians.

I found the Python API Geometry Class docs to be a bit confusing as they suggested a result being a tuple of xy coords and the distance.

Then I found this https://community.esri.com/t5/arcgis-api-for-python-questions/need-query-point-and-distance-clarific...

which describes the actual result object (straight from the arcpy function). 😐

0 Kudos
StuartMoore
Frequent Contributor

 

this is the code:

for xbsg in bsgPoints:
    print(xbsg.attributes["objectid"])
    print(xbsg)
    access_point = Point(xbsg.geometry)
    access_res = access_polyline.query_point_and_distance(access_point)
    if (access_res):
        print(access_res)
        print("--++--")
        print(access_res[0].JSON)
    print("----")

this is the code's closest point
StuartMoore_1-1736431031199.png

but i was expecting this as the closest point:

StuartMoore_2-1736431090257.png

and this is the distances returned:

StuartMoore_3-1736431224694.png

i also found that post and it suggests that the distance is the second item so 

0.001106969364200805

 

 

0 Kudos
DavidPike
MVP Frequent Contributor

From the geometry methods doc and the doc screenshot in that thread, the 2nd value appears to be the distance from the line start along the line.  I think it's the 3rd value you're looking for.  Without knowing more about your data etc. I'm not sure what may be happening with the results.

I'd print out the JSON of the inputs also and maybe something with the SRID/WKID might show up.  

https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/geometry.htm

Clubdebambos
MVP Regular Contributor

You can get the information for the output tuple from the ArcPy Polyline documentation.

Clubdebambos_0-1736437329755.png

 

~ learn.finaldraftmapping.com
0 Kudos
StuartMoore
Frequent Contributor

this is the geometry of one of the polylines

{"geometry": {"paths": [[[-1.04626706475298, 52.5312679471987], ........ [-1.0447855316579, 52.5304270718345]]], "spatialReference": {"wkid": 4326, "latestWkid": 4326}}


this is the geometry for one of the points

{"geometry": {"x": -1.0453711186340822, "y": 52.53063399067387, "spatialReference": {"wkid": 4326, "latestWkid": 4326}}

 

thanks for the help

Stu

0 Kudos
DavidPike
MVP Frequent Contributor

OK that's WGS84 and the lengths I guess are decimal degrees.  Unless there's another Geodesic Method available I think you'd really need to project the coordinates into a planimetric coordinate system to get a meaningful output.