Python を使って、地図上のある座標が、4つの座標の中に含まれるかどうかを判定したい。
↓こちらのサイトを参考にしました。
#!/usr/bin/env python #-*- coding:utf-8 -*- import numpy as np def main(): n = 1 x = np.ndarray(n, dtype=float) y = np.ndarray(n, dtype=float) inside = np.ndarray(n, dtype=bool) x = [135.4978645] y = [34.7062076] inside[:] = False x1, y1 = [], [] x1.append(135.496926) y1.append(34.7065168) x1.append(135.497527) y1.append(34.7033239) x1.append(135.4998448) y1.append(34.7040295) x1.append(135.498965) y1.append(34.7067814) # check the points inside the polygon for i, (sx, sy) in enumerate(zip(x, y)): print("i = " + str(i)) print("x = " + str(x)) print("y = " + str(y)) print("sx = " + str(sx)) print("sy = " + str(sy)) inside[i] = inpolygon(sx, sy, x1, y1) # True or False print(inside[i]) def inpolygon(sx, sy, x, y): ''' x[:], y[:]: polygon sx, sy: point ''' np = len(x) inside = False for i1 in range(np): i2 = (i1 + 1) % np if min(x[i1], x[i2]) < sx < max(x[i1], x[i2]): # a = (y[i2]-y[i1])/(x[i2]-x[i1]) # b = y[i1] - a*x[i1] # dy = a*sx+b - sy # if dy >= 0: if (y[i1] + (y[i2] - y[i1]) / (x[i2] - x[i1]) * (sx - x[i1]) - sy) > 0: inside = not inside return inside if __name__ == "__main__": main()