2

I'm trying to script something with Python and GDAL/OGR and Shapely.

What I have:

  • two LineString geometries: "geom1" and "geom2"
  • I made an Intersection: "intersection = geom1.Intersection(geom2)"

Now I wanted to check, if the intersection-Point is either the starting- or endingpoint (or none of both) of one of the input geometries.

What I did:

from shapely.geometry import Point    
point_start_1 = Point(geom1.GetPoint_2D(0))
point_end_1 = Point(geom1.GetPoint_2D(geom1.GetPointCount()-1))
point_start_2 = Point(geom2.GetPoint_2D(0))
point_end_2 = Point(geom2.GetPoint_2D(geom2.GetPointCount()-1))

The following code won't work:

if intersection.Intersects(point_start_1):
            print "intersection and Starting Point of geom1 is the same Point"

The Error Message is:

"TypeError: in method 'Geometry_Intersects', argument 2 of type 'OGRGeometryShadow *'"

My Question is:

What is a OGRGeometryShadow and why can't I intersect it with another Geometry? When I print the start_point_1 it says: "POINT (6.331836999999999 53.6154012)" So it looks like a normal geometry. But as I said "Intersects"-method doesn't work.

1 Answer 1

2

In your solution you mix geometries and predicates of ogr and Shapely

With Shapely only (intersection(), intersects())

from shapely.geometry import Point, LineString, mapping
geom1  = LineString([(0, 0), (0, 1)])
geom2  = LineString([(1, 1), (-1,-1)])
intersection = geom1.intersection(geom2)
# geo_interface -> GeoJSON
mapping(intersection)
{'type': 'Point', 'coordinates': (0.0, 0.0)}
# Shapely geometry
pt1 = [Point(pt) for pt in list(geom1.coords)]
point_start_1 = pt1[0]
.....
# Shapely predicate
intersection.intersects(point_start_1)
True

With ogr only (Intersection(), Intersects()):

from osgeo import ogr
geom1 = ogr.CreateGeometryFromWkt('LINESTRING (0 0, 0 1)')
geom2 = ogr.CreateGeometryFromWkt('LINESTRING (1 1, -1 -1)')
# intersection with ogr
intersection_ogr = geom1.Intersection(geom2)
# geo_interface -> GeoJSON
intersection_ogr.ExportToJson()
'{ "type": "Point", "coordinates": [ 0.0, 0.0 ] }'
# ogr geometry
point_start_1 = ogr.Geometry(ogr.wkbPoint)
point_start_1.AddPoint(geom1.GetPoint_2D(0)[0],geom1.GetPoint_2D(0)[1])
....
# ogr predicate
intersection_ogr.Intersects(point_start_1)
True

In your solution, you use Shapely geometries:

point_start_1 = Point(geom_ogr1.GetPoint_2D(0))
....

with a binary predicate of ogr -> error

intersection_ogr.Intersects(point_start_1) 
TypeError: in method 'Geometry_Equal', argument 2 of type 'OGRGeometryShadow *'
1
  • 2
    What gene means to say is that you can't mix osgeo.ogr and Shapely like that. Their geometry objects are not interchangeable.
    – sgillies
    Commented Nov 22, 2014 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.