# How to efficiently compute if point is on polyline?

I have a CSV file with data in GeoJSON format with 10k+ lines with various open / public data I want to use. So far it's in a file but later it may be in a SQLite database or even better be kept in RAM. This is to be used in a mobile app.

There are many columns in that file but what interests me are the geo shape column which is made of polylines (linestring with several points) and the column called "geo center"(it is has the center between start and end points of the polyline).

I want to know if a location (provided by the mobile device) is on any polylines defined in the file.

So my first thought is to filter all geo centers that are near enough to the given location, and then for each filtered polylines, check if the given location belongs the series of point couples.

Eg : location is L

Polyline 1 has 3 geo points X Y Z

My algorithm is (after filtering step) :

Is L on [X Y] ? If not is L on [Y Z] ? ...

Is it the most efficient way to tell if a point is on a polyline or is there any other way ?

• 1st import your data into a spatial database, then use that to solve problem. Commented Oct 4, 2019 at 10:21
• 1 million features in a GeoJSON file is a hell of a lot. Also, is there more to this? GeoJSON features only have a single geometry, but you talk about two and refer to them as "columns"? Commented Oct 4, 2019 at 10:23
• Point "on" a line is impossible to compute with some IEEE floating-point values. Far better to compute point near a line with some epsilon. For efficiency, you can do what most GIS packages do with spatial indexes and test in the envelope before attempting distance. Commented Oct 4, 2019 at 12:05
• @IanTurton thanks I found SpatiaLite that I will try out and learn. Commented Oct 4, 2019 at 12:56
• @RichardLaw the file is actually a csv with data in GeoJSON format. That's why I talked about columns (related to the csv file). Commented Oct 4, 2019 at 12:58

So after some research and thanks to the comments above, here is how I solved my problem (for the record).

1) Use a spatial database (e.g. Spatialite)

2) Import the interesting data from the CSV file into Spatialite db (Spatialite can also read CSV files directly.

2.1) Use GeomFromText to import data provided as string

2.2) BEWARE that the points are always provided with latitude - longitude in the same order (in my case one column was in lat - long where the polylines were in long - lat)

3) Use the PtDistWithin function to know if the point belongs to the road segment

Example :

SELECT * FROM MyTable WHERE PtDistWithin( GeomFromText('POINT (long1 lat1)', 4326), GeomFromText('LINESTRING (long2 lat2, long3 lat3, long4 lat4)', 4326), dstInMeter)

Distance is in meter if 4326 is used in all geometries.