# How to find measure from geometry using PostGIS or GeoServer (Linear Referencing)

I'm using OpenLayers with GeoServer and PostGIS. I'm looking for a way to get measure from geometry. I mean if a user clicks on a feature as line route on the map (geometry), we can find it's measure or distance from begin origin of the route.

It is clear that sometimes, origin point of a route has a measure other than zero. On Esri ArcGIS we have this functionality using location information on Linear Referencing features.

Is there any way to find it using postGIS or GeoServer functionalities?

• distance from start of a line to the coordinates of the click event? – ThingumaBob Apr 14 '19 at 9:50
• yes.....I'm looking for a way to find distance from start by click on map and also If I have a distance on a route, I can find its position on the map – Ali Fasihi Apr 15 '19 at 7:17

Thanks a lot dear Maximilien... actually I am looking for a way under the GIS Linear Referencing concept which measure and distance have a different mean than general geometric concept. Anyway finally I found the solution as below:

problem 1: measure from geometry we have a coordinate from user click event as geometry like: POINT(5 5)

solution:

``````SELECT ST_InterpolatePoint('LINESTRING M (0 0 0, 10 0 20)', 'POINT(5 5)');
``````

response: 10

problem 2: geometry from measure user looking for a location on a route having measure like 45

solution:

``````SELECT ST_AsText((ST_Dump(the_geom)).geom)
FROM
(SELECT ST_LocateAlong(
ST_GeomFromText('LINESTRINGM (0 0 0, 10 0 20, 12 0 40, 20 0 50, 21 0 70)'), 45) As the_geom) As foo;
``````

response: POINT M (16 0 45)

i'll try to produce a wide answer as the question is still unclear for me. Feel free to refine your question.

St_Distance combined with St_StartPoint of your linestring should do the trick you are looking for in PostGis/PostGres.

Basic sample of code :

``````Select St_Distance(geom_point_click,St_StartPoint(geom_linestring))
from table_you_need
``````

will give you the distance

if you want to place a point with a given distance along a Linestring, you will also need St_linelocatePoint and St_length in Postgis and do a somewhat similar query.

Now the problem is that you need to retrieve the click's coordinate to produce a point geom if you want to do it in PostGis.

Another way of thinking is to handle this event on the map itself, but i don't know what you are doing. If you are using OpenLayer or Leaflet to produce the map, you should write some JavaScript to do what you want instead of going back to your DataBase.