distance from a point to the nearest edge of a polygon

2024/11/16 11:33:37

in the below code i want to calculate the distance from a point to the nearest edge of a polygon.as shown in the results section below, the coordinates are provided.the code posted below shows how i find the distance from a point to the neatrest edge of a polygon. at run time, and as shown below in restults section, for the give point and geometry, the distance from postgis is equal to 4.32797817574802 while the one calculated from geopandas gives 3.8954865274727614e-05

please let me know how to find the distance from a point to nearest edge of a polygon.

code

poly = wkt.loads(fieldCoordinatesAsTextInWKTInEPSG25832)
pt = wkt.loads(centerPointointAsTextInWKTInEPSG25832)
print(poly.distance(pt)))

results:

queryPostgreSQLForDistancesFromPointsToPolygon:4.32797817574802#result from postgis using st_distance operator
centerPointointAsTextInWKTInEPSG4326:POINT(6.7419520458647835 51.08427961641239)
centerPointointAsTextInWKTInEPSG25832:POINT(341849.5 5661622.5)
centerPointointAsTextInWKTInEPSG4326:POINT(6.7419520458647835 51.08427961641239)
fieldCoordinatesAsTextInWKTInEPSG25832:POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))
fieldCoordinatesAsTextInWKTInEPSG4326:POLYGON((6.741879696309871 51.08423775429969,6.742907378503366 51.08158745820981,6.746964018740842 51.08233499299334,6.746152690693346 51.08440763989611,6.741879696309871 51.08423775429969))
poly.distance(pt):3.8954865274727614e-05#result from geopandas
Answer
  • your code works. It's approx 7000km from Belgium to Ethiopia
  • are you sure your data is correct? Have built a plotly graph to show where buffered polygon, polygon centroid and point are located in EPSG:4326 CRS
from shapely import wkt
import geopandas as gpd
import plotly.express as px
import json# queryPostgreSQLForDistancesFromPointsToPolygon:4.32797817574802#result from postgis using st_distance operator
centerPointointAsTextInWKTInEPSG4326 = "POINT(6.7419520458647835 51.08427961641239)"
centerPointointAsTextInWKTInEPSG25832 = "POINT(341849.5 5661622.5)"
centerPointointAsTextInWKTInEPSG4326 = "POINT(6.7419520458647835 51.08427961641239)"
fieldCoordinatesAsTextInWKTInEPSG25832 = "POLYGON ((5622486.93624152 1003060.89945681,5622079.52632924 1003170.95198635,5622126.00418918 1003781.73122161,5622444.73987453 1003694.55868486,5622486.93624152 1003060.89945681))"
fieldCoordinatesAsTextInWKTInEPSG4326 = "POLYGON((6.741879696309871 51.08423775429969,6.742907378503366 51.08158745820981,6.746964018740842 51.08233499299334,6.746152690693346 51.08440763989611,6.741879696309871 51.08423775429969))"
# poly.distance(pt):3.8954865274727614e-05#result from geopandaspoly = wkt.loads(fieldCoordinatesAsTextInWKTInEPSG25832)
pt = wkt.loads(centerPointointAsTextInWKTInEPSG25832)print(poly.distance(pt)/10**3)# let's visualize it....
gpoly = (gpd.GeoDataFrame(geometry=[poly], crs="EPSG:25832").buffer(10 ** 6).to_crs("EPSG:4326")
)
gpoly.plot()
gpt = gpd.GeoDataFrame(geometry=[pt, poly.centroid], crs="EPSG:25832").to_crs("EPSG:4326"
)
px.scatter_mapbox(gpt.assign(dist=poly.distance(pt)/10**3),lat=gpt.geometry.y,lon=gpt.geometry.x,hover_data={"dist":":.0f"},
).update_layout(mapbox={"style": "carto-positron","zoom": 4,"layers": [{"source": json.loads(gpoly.to_json()),"below": "traces","type": "fill","color": "red",}],}
)

enter image description here

https://en.xdnf.cn/q/120336.html

Related Q&A

Is there a netcat alternative on Linux using Python? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

keras/tensorflow model: gradient w.r.t. input return the same (wrong?) value for all input data

Given a trained keras model I am trying to compute the gradient of the output with respect to the input. This example tries to fit the function y=x^2 with a keras model composed by 4 layers of relu act…

Pandas: get json from data frame

I have data framemember_id,2015-05-01,2015-05-02,2015-05-03,2015-05-04,2015-05-05,2015-05-06,2015-05-07,2015-05-08,2015-05-09,2015-05-10,2015-05-11,2015-05-12,2015-05-13,2015-05-14,2015-05-15,2015-05-1…

Python - Statistical distribution

Im quite new to python world. Also, Im not a statistician. Im in the need to implementing mathematical models developed by mathematicians in a computer science programming language. Ive chosen python a…

How to add data in list below?

i have a list :List = [[[1,2],[2,4]],[[1,4],[4,8]],[[53,8],[8,2],[2,82]]]That i want add reverse data to listTo be:[[[1,2],[2,4],[2,1],[4,2]],[[1,4],[4,8],[4,1],[8,4]],[[53,8],[8,2],[2,82],[8,53],[2,8]…

Storing lists within lists in Python

I have a question about accessing elements in lists. This is the code: movies = ["The Holy Grail", 1975, "Terry Jones and Terry Gilliam", 91,["Graham Champman", ["Mic…

getting ZeroDivisionError: integer division or modulo by zero

I had written a simple pascal triangle code in python but I am getting a errordef factorial(n):c=1re=1for c in range(n):re = re * c;return(re)print "Enter how many rows of pascal triangle u want t…

How to scrape images from a website and display them on html file?

I am scraping images from https://www.open2study.com/courses I got all the image sources but dont know how to display the images (instead of links) on a table with 2 column ( one column for title and o…

Multiple files comparing using python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

parsing interactive broker fundamental data

Ive successfully pulled data from IB using the api. It comes in XML format and it looks like this...<TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" report…