Search code examples
node.jscordovageojson

Search on GeoJson files


I developing a app based on Cordova with a geojson file and i need know if have a way to check if my current geolocation (latitude and logitude) is inside a geojson:linestring. This app work offline.


Solution

  • Here is a solution using jq (https://stedolan.github.io/jq).

    Given a valid geojson object, report(latLong) as defined here will return true each time an object with "type" equal to "LineString" and "coordinates" having the specified latLong is encountered as the geojson object is traversed.

    def report(latLong):
      if type == "object"
         and .type == "LineString"
         and (.coordinates | type) == "array"
         and (.coordinates | index([latLong])) then true
      elif type == "array" or type == "object" then .[] | report(latLong)
      else empty
      end;
    

    One way to use this definition is to put it into a file, say geojson.jq, together with the following line:

    report($latLong | fromjson)
    

    Here is an example that assumes the geojson object at http://geojson.org/geojson-spec.html#examples is in a file called geojson.json:

    $ jq --arg latLong "[103.0, 1.0]" -f geojson.jq geojson.json
    
    true
    

    There are other ways to use the above-defined report/1, and of course there are variants of report/1 that could be defined, e.g. to yield something other than true.