私はポイントからポリゴンの最も近いポリゴンの端までの距離を計算する効率的な方法を見つけようとしています。私は滑らかにこれには完璧だと思ったが、ポイントがポリゴンの外にあるときにのみ距離を計算する。私はopencvについて考えましたが、これには関数が組み込まれていますが、整数座標が必要です(データを離散化できません)。ポリゴンは凸であるとは限りません。ポイントからポリゴンまでの距離(内部の場合)
答えて
[マルチ]ポリゴンの.boundary
プロパティを使用して、LinearRingsの[Multi] LineStringを返し、次に.distance
プロパティを使用します。外部リングだけが必要で、内部リングがない場合は.exterior
プロパティを使用することもできます。例えば。
from shapely import wkt
poly = wkt.loads('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))')
pt = wkt.loads('POINT(20 20)')
# The point is in the polygon, so the distance will always be 0.0
poly.distance(pt) # 0.0
# The distance is calculated to the nearest boundary
poly.boundary.distance(pt) # 4.47213595499958
# or exterior ring
poly.exterior.distance(pt) # 4.47213595499958
結果は、この例に.boundary
と.exterior
についても同様であるが、他の実施例のために変更することができます。
この最小距離点の座標を見つける必要がある場合は、どうすればよいですか? –
@AlexRosenfeld [線形参照メソッド](http://toblerity.org/shapely/manual.html#linear-referencing-methods): 'poly.exterior.interpolate(poly.exterior.project(pt) ))。wkt'は 'POINT(18 16)'を返します。 –
私はcythonで線とポリゴンの交点(エッジによって与えられる)を得るためのコードをいくつか持っています。あなたがここで待っていてはいけないなら、それはです。今、船の船体の境界線と水線の交差点の唯一の生コードです。今日のあなた自身に必要ないただきまし実装する必要があり:P ...
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.interpolate as sci_itp
import scipy.integrate as sci_itg
cimport numpy as np
cimport cython
from CROSS_SECTIONS_universal import *
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
ITYPE = np.int
ctypedef np.int_t ITYPE_t
ITYPEP = np.intp
ctypedef np.intp_t ITYPEP_t
ITYPE1 = np.int64
ctypedef np.int64_t ITYPE1_t
BTYPE = np.uint8
ctypedef np.uint8_t BTYPE_t
ctypedef Py_ssize_t shape_t
BOTYPE = np.bool_
cdef double[:,:] waterLine = np.array([[0,0],[0,0]],dtype=DTYPE)
cdef double[:,:] crossLine = np.array([[0,0],[0,0]],dtype=DTYPE)
cdef double[:,:] line
cdef double[:] sectionLinePoint = np.full((2),np.nan,dtype=DTYPE)
cdef double[:,:] sectionLengthPOINTS = np.full((2,2),np.nan,dtype=DTYPE)
cdef double[:,:] sectionBreadthPOINTS = np.full((2,2),np.nan,dtype=DTYPE)
cdef double sectionLength,sectionBreadth
cdef point_side_of_line(double ax,double ay,double bx,double by,double cx,double cy):
""" Returns a position of the point c relative to the line going through a and b
Points a, b are expected to be different
"""
cdef double s10x,s10y,s20x,s20y,denom
cdef bint denomIsPositive,denomIsNegative
cdef int side
s10x,s10y=bx-ax,by-ay
s20x,s20y=cx-ax,cy-ay
denom = s20y*s10x - s10y*s20x
denomIsPositive,denomIsNegative=denom>0,denom<0
side=1 if denomIsPositive else (-1 if denomIsNegative else 0)
return side
cdef is_point_in_closed_segment(double ax,double ay,double bx,double by,double cx,double cy):
""" Returns True if c is inside closed segment, False otherwise.
a, b, c are expected to be collinear
"""
cdef bint pointIn
if ax < bx:
pointIn=ax <= cx and cx <= bx
return pointIn
if bx < ax:
pointIn=bx <= cx and cx <= ax
return pointIn
if ay < by:
pointIn=ay <= cy and cy <= by
return pointIn
if by < ay:
pointIn=by <= cy and cy <= ay
return pointIn
pointIn=ax == cx and ay == cy
return pointIn
cdef closed_segment_intersect(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy):
""" Verifies if closed segments a, b, c, d do intersect.
"""
cdef int s1,s2
if (ax is bx) and (ay is by):
assert (ax is cx and ay is cy) or (ax is dx and ay is dy)
if (cx is dx) and (cy is dy):
assert (cx is ax and cy is ay) or (cx is bx and cy is by)
s1,s2 = point_side_of_line(ax,ay,bx,by,cx,cy),point_side_of_line(ax,ay,bx,by,dx,dy)
# All points are collinear
if s1 is 0 and s2 is 0:
assert \
is_point_in_closed_segment(ax,ay,bx,by,cx,cy) or \
is_point_in_closed_segment(ax,ay,bx,by,dx,dy) or \
is_point_in_closed_segment(cx,cy,dx,dy,ax,ay) or \
is_point_in_closed_segment(cx,cy,dx,dy,bx,by)
# No touching and on the same side
if s1 and s1 is s2:
assert False
s1,s2 = point_side_of_line(cx,cy,dx,dy,ax,ay),point_side_of_line(cx,cy,dx,dy,bx,by)
# No touching and on the same side
if s1 and s1 is s2:
assert False
cdef find_intersection(double[:,:] L1,double[:,:] L2, double[:] intersectionPoint) :
cdef double ax,ay,bx,by,cx,cy,dx,dy
cdef double s10x,s10y,s32x,s32y,s02x,s02y
cdef double tNumer,t,denom
ax,ay=L1[0]
bx,by=L1[1]
cx,cy=L2[0]
dx,dy=L2[1]
closed_segment_intersect(ax,ay,bx,by,cx,cy,dx,dy)
s10x,s10y=bx-ax,by-ay
s02x,s02y=ax-cx,ay-cy
s32x,s32y=dx-cx,dy-cy
denom = s10x * s32y - s32x * s10y
tNumer = s32x * s02y - s32y * s02x
t = tNumer/denom
intersectionPoint[0]=ax+t*s10x
intersectionPoint[1]=ay+t*s10y
cdef section_length_breadth(double[:,:] planeSectionCLUSTER,double x0,double x1, double y0, double y1):
cdef int counterL=0,counterB=0
x01=(x0+x1)/2.
crossLine[0,0]=x01
crossLine[1,0]=x01
crossLine[0,1]=y0-0.1
crossLine[1,1]=y1+0.1
waterLine[0,0]=x0-0.1
waterLine[1,0]=x1+0.1
for i in range(1,len(planeSectionCLUSTER)):
line=planeSectionCLUSTER[i-1:i+1]
plt.plot(line[:,0],line[:,1],'c-',lw=3)
try:
try:
find_intersection(line,waterLine,sectionLinePoint)
assert sectionLinePoint[0] not in sectionLengthPOINTS[:,0]
sectionLengthPOINTS[counterL]=sectionLinePoint
counterL+=1
except AssertionError:
find_intersection(line,crossLine,sectionLinePoint)
assert sectionLinePoint[1] not in sectionBreadthPOINTS[:,1]
sectionBreadthPOINTS[counterB]=sectionLinePoint
counterB+=1
except AssertionError:
pass
print '#'
print [[j for j in i] for i in sectionLengthPOINTS]
print [[j for j in i] for i in sectionBreadthPOINTS]
plt.plot(waterLine[:,0],waterLine[:,1],'b--',lw=3)
plt.plot(sectionLengthPOINTS[:,0],sectionLengthPOINTS[:,1],'ko-',lw=3)
plt.plot(crossLine[:,0],crossLine[:,1],'b--',lw=3)
plt.plot(sectionBreadthPOINTS[:,0],sectionBreadthPOINTS[:,1],'ro-',lw=3)
return 0,0
- 1. ポリゴン内の点からポリゴンの端までの距離
- 2. ポイントとポリゴンとの間の距離R
- 3. 距離ポリゴン内のapi
- 4. Python:VTKポイントからvtkLineSourceまでの距離
- 5. ポイントからラインセグメントの距離にポイント
- 6. レイヤ内の1つのポリゴンから他のポリゴンまでの最小距離を見つけますか?
- 7. バッファー距離内でポイントからマルチポリゴンまでの距離を教える方法R
- 8. ポイントフィーチャから最近傍ポリゴンまでの距離R
- 9. ポリゴン内部のポイントを取得
- 10. 2つのポイント間の距離と距離が最も離れています
- 11. PostGISのポリゴン間の直交距離
- 12. 2Dポリゴン内のポイント
- 13. Pyproj点と点とポリゴン間の距離
- 14. 場所からの最小総距離のポイントを見つけるアルゴリズム
- 15. ポリラインセグメントアルゴリズムへの3Dポイントの距離
- 16. ポイントとパスサーバー側の間の距離
- 17. 計算フュージョンテーブルのポイント間の距離
- 18. 特定の距離内のポイントを選択
- 19. 指定された円内のDjangoポイントの距離
- 20. R:GISデータ内メートルのポイントとライン間の距離
- 21. ポイントからの距離にズームエフェクトベースを作成するには
- 22. タップからの距離を計算するポイントcom.jjoe64.graphview.GraphView
- 23. Paper.jsポイントとパス間の距離
- 24. 最小距離のランダムなポイント[オクターブ]
- 25. 現在の場所から注釈までの距離。 (Firebase)
- 26. ポリゴンから地理距離を計算する方法は?
- 27. sklearn:ポイントから最寄りのクラスタまでの距離を取得
- 28. nodejs:場所からユーザーまでの距離を計算する
- 29. 距離データが大きいデータの場合、距離が戻ってこない
- 30. nxnポイントの距離行列の計算を減らす
は、その後、あなたは(http://mathworld.wolfram.com/Point-LineDistance2すべてのエッジのためのポイント・ラインの距離を計算する必要があります-Dimensional.html)、私はあなたのケースにはショートカットがないと思っています(しかし、あなたは数学のサイトに尋ねることができます)。 – thebjorn
距離変換を使用できませんか? – Miki
@thebjornそれほど複雑ではないですか?点線距離は、線分ではなく点と*線*の最短距離を測定するため、法線(テストポイントを通る)が実際にエッジと交差しないエッジを最初に除去する必要があります、いいえ? – KevinOrr