[GIS] create temporary point geometry feature from coordinates in OGR with Python

geometrygeoprocessingogrshapefile

I have two shapefiles containing two types of geometry features: one contains polygons and the other linestrings.

Because I'd like to use the Within method to check whether the end(or start) of a linestring falls within a polygon located in the other shapefile, I need to access the first and last points (as geometry features).
At the moment I iterate through the linestring feature and can identify the points using geometry.GetPoint(x) which returns the x and y coordinates of that point.

But when I'd like to use within to check whether a given point falls in the polygon object I can't because as Paolo explained in this question I need the geometry reference (using GetGeometryRef())of a point which can be retrieved like:

...
pointshp = driver.Open('test.shp')
pointlyr = pointshp.getLayer(0)
point = pointlyr.GetNextFeature()
point_geom = point.GetGeometryRef()
point_geom.Within(polygon_geom)

but I'm actually opening a shp that has linestring features. So after skimming through Utah State University GIS slides, I came up with this:

def within(pointcoord):
driver = ogr.GetDriverByName('test.shp')
ds = driver.CreateDataSource('test.shp')
layer = ds.CreateLayer('test',geom_type=ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id',ogr.OFTInteger)
layer.CreateFiled(fieldDefn)
featureDefn = layer.GetLayerDefn()
feature = ogr.feature(featureDefn)

point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointcoord[0],pointcoord[1])
feature.SetGeometry(point)
feature.SetField('id',1)
layer.CreateFeature(feature)
pointlyr = layer.GetLayer(0)
point = pointlyr.GetNextFeature()
point_geom = point.GetGeometryRef()
for x in xrange(len(polygons))
        point_geom.Within(Network.polygons[x].GetGeometryRef())

Which looks really inefficient to me. So just for the sake of using a method(within) I need to create not only a layer, but a shapefile, when all I really need is a simple point feature from which I can get the geometry reference to compare it with the one of the polygons.

The question is, how to create a point feature (temporary one, to be destroyed) from x and y coordinates? Or is there a simpler way of doing that than the example above?

On another hand, is there any other way of getting the geometry reference of the start or end point (as a geometry feature) of a linestring feature?

Best Answer

Using the coordinates of the linestring geometry feature it is possible to simply create a geometry object like:

 point = ogr.Geometry(ogr.wkbPoint)
 point.AddPoint(g.GetX(j), g.GetY(j))#where j is the iteration in the linestring i.e. 0

that can later be used with: point.Within(polygon)