1

I am working in Python 3.7 using ogr to check if a point is inside a polygon. Here I put the code. The result in geom is not compatible with the tuple list data type used by the point_inside function.

How can this information be made compatible?

from osgeo import ogr

def point_inside_polygon(x,y,poly):

n = len(poly)
inside =False

p1x,p1y = poly[0]
for i in range(n+1):
    p2x,p2y = poly[i % n]
    if y > min(p1y,p2y):
        if y <= max(p1y,p2y):
            if x <= max(p1x,p2x):
                if p1y != p2y:
                    xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                if p1x == p2x or x <= xinters:
                    inside = not inside
    p1x,p1y = p2x,p2y

return inside

file = ogr.Open("C:/Programas/Python/salida.shp/MSK_CLOUDS_B00.shp") shape = file.GetLayer() feature = shape.GetFeature(0)

for feature in shape:

#geom = feature.GetGeometryRef() geom = feature.geometry()

print (geom)

print(point_inside_polygon(-90.2,14.3,geom) )

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389

2 Answers2

1

Why not use the contains methods on the polygon (geom)?

p1 = Point(-90.2, 14.3)

for feature in shape: geom = feature.geometry()

 if geom.contains(p1):
      print(geom)

Ian Turton
  • 81,417
  • 6
  • 84
  • 185
0

EUREKA

for feature in shape:

#geom = feature.GetGeometryRef() geom = feature.geometry() json_geom = feature.ExportToJson()

geom = json.loads(json_geom)

poly=(geom['geometry'] ['coordinates'])

poly_a=(poly[0])

print(point_inside_polygon(x,y,poly_a))

print ("------")

Ian Turton
  • 81,417
  • 6
  • 84
  • 185