0

I'm interested in finding the midpoint on a linestring using OGR and Python. This can be a line with more segments. Could I use the ST_LINE_INTERPOLATE_POINT in Python using the ExecuteSQL() method?

If so, could someone perhaps help me formulate the correct SQL query? Or is there an alternative option? I considered using Centroid but the centroid can also lie outside of the geometry.

---- UPDATE ----
I have QGIS installed on Windows and use the OSGeo4W shell. I made the following Python code but unfortunately it doesn't work, geom is seen as a NoneType.

from osgeo import ogr
outdriver=ogr.GetDriverByName('SQLITE')
source=outdriver.CreateDataSource('temp')
tmp=outdriver.Open('temp')
layer = tmp.ExecuteSQL("""SELECT ST_AsText(ST_Line_Interpolate_Point(ST_GeomFromText('LINESTRING ( 90 379, 99 278, 189 224, 297 193, 403 189, 459 202 )'),0.5))""")
geom = layer.GetNextFeature().GetGeometryRef()
print geom.ExportToWkt()
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Jul 22, 2015 at 8:23

2 Answers 2

3

As described in https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html the function is used as

ST_Line_Interpolate_Point( line Curve , fraction Double precision ) : Point

An example that can be tested with SQL command line for example with spatialite-gui. Fraction=0.5 finds the midpoint.

SELECT ST_AsText(
ST_Line_Interpolate_Point
(ST_GeomFromText('LINESTRING ( 90 379, 99 278, 189 224, 297 193, 403 189, 459 202 )'),0.5))
POINT(222.434071 214.403183)

You can use that query as a template for your ExecuteSQL(). Remember that you must use the SQLite SQL dialect and GDAL must be compiled with such Spatilite version that has the ST_Line_Interpolate_Point function.

answered Jul 25, 2015 at 10:43
0
0

The PyQGIS geometry object (QgsGeometry) has an interpolate function. The OGR geometry object I am using does not. So I will use QgsGeometry instead.

See: https://woostuff.wordpress.com/2012/08/05/generating-chainage-distance-nodes-in-qgis/

ThomasG77
31.7k1 gold badge56 silver badges96 bronze badges
answered Sep 8, 2015 at 10:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.