2

How can I find the closest feature(coordinates) in a json file from a set lat lon using openlayers. Currently, I get the centroid point(lat,lon) from the selected polygon feature ok, but I Would also like to return the closest feature "sid" value from the json file below.

Here is the contents of the json file:

 { "type": "FeatureCollection",
    "features": [
      {
  "type":"Feature",
    "geometry":{"type":"Point","coordinates":[146.9509,-36.069]},
      "properties":{"name":"ALBURY AIRPORT AWS","sid":72160,"rain":[0,0,0,0,0,0,0,1.2,0,0]}
},
{
  "type":"Feature",
    "geometry":{"type":"Point","coordinates":[142.0867,-34.2358]},
      "properties":{"name":"MILDURA AIRPORT","sid":76031,"rain":[0,0,0,0,0,0,0,0,0,0]}
},
{
  "type":"Feature",
    "geometry":{"type":"Point","coordinates":[142.3158,-35.0694]},
      "properties":{"name":"OUYEN (POST OFFICE)","sid":76047,"rain":[0,0,0,0,0,0,0,0,0,0]}
},
{
    "type":"Feature",
      "geometry":{"type":"Point","coordinates":[142.004,-35.1201]},
          "properties":{"name":"WALPEUP RESEARCH","sid":76064,"rain":[0,0,0,0,0,0,0,0,0,0]}
},
{
  "type":"Feature",
    "geometry":{"type":"Point","coordinates":[143.5416,-35.3766]},
      "properties":{"name":"SWAN HILL AERODROME","sid":77094,"rain":[0,0,0,0,0,0,0,0,0,0]}
}]}
3

I think that the function distance to can resolve your problem. here is the openlayers documentation : DistanceTo

And try to use this function

  function nearest_feature(pointA) {
    var minDistance = vector.features[0].geometry.distanceTo(pointA, {details: false, edge: true});
    var index =0;
    for (var i = 1; i <= vector.features.length - 1; i++) {
        var dist = vector.features[i].geometry.distanceTo(pointA, {details: false, edge: true});
        if (dist < minDistance) {
            index = i;
            minDistance = dist; 
        }
    }
     return vector.features[index].attributes['sid'];
 }

vector is your vector layer that contains your features

pointA is your centroid point that you return it should be an OpenLayers.Geometry.Point

  • +1 for noting DistanceTo, but what is the geom1 variable, there? Is there a reason why you're doing the 0-index assignment out of the loop, then starting the loop with the 1-index? I think it would be cleaner to perform 100% of the analysis within your loop. Mostly I think this is a programmer style/preference, though. – elrobis Apr 23 '14 at 13:45
  • no it's just a error you just delete it here the correction var dist = vector.features[i].geometry.distanceTo(pointA, {details: false, edge: true}); – b-eyes Apr 23 '14 at 14:08
  • n/m my remarks about the initial out-of-loop assignment. As a thought exercise I tried rewriting the function, and now I appreciate the way you wrote it better. – elrobis Apr 23 '14 at 14:08
  • I corrected the error of geom1 . I'm a begginer in openlayers and I hope that code may help you – b-eyes Apr 23 '14 at 14:13
  • ah.. I may see another issue, though, minDistance never gets a new value assignment, meaning every point is compared to the distance measured for the first point, and not necessarily the last minimum distance measured. I think this can be fixed easily. In your if block, you just need to set minDistance = dist; before the next iteration.. yes? – elrobis Apr 23 '14 at 14:15
1

If you are willing to use another library I'd suggest JavaScript Topology Suite (jsts). The steps would be similar to this:

  1. Create a OpenLayers.Geometry.Point from your set lat long and convert it to a jsts geometry using jsts.io.OpenLayersParser (call it pointA).
  2. Iterate over your JSON.features.
  3. Create an OpenLayers.Geometry.Point and jsts geometry from the current feature (call it pointB).
  4. Carry out the jsts distance function against pointA and pointB.

The first time through the iteration set minDistance and selectedFeatures to the result of the distance function and the current feature. Subsequent iterations only set minDistance and selecteFeature if the distance is less than the minDistance you have.

I used this page to get an idea how to actually use the jsts library:

http://gis.ibbeck.de/ginfo/apps/OLExamples/OL210/JSTS_Example/jsts_example.asp

  • Thanks Dave, that is definitely something for me to consider next time. Looks like I could use this in place of a number of postgis functions. – Haydlew Apr 24 '14 at 3:51
  • I'm not sure, but it seems that OpenLayers.geometry.distanceTo may only work on planar coordinate systems (not longitude/latitude as it seems you have). The docs (dev.openlayers.org/apidocs/files/OpenLayers/…) note "on the x-y plane." – Dave Lowther Apr 28 '14 at 10:36
  • Here is a link to a pure JS distance function for geographic coordinates: stackoverflow.com/questions/4102520/… – Dave Lowther Apr 28 '14 at 10:37
  • yeah, I had to do a transformations for it return correct distances. – Haydlew Apr 29 '14 at 23:43
0

Use Turfjs' nearest function.

var nearestFeature = turf.nearest(targetFeature, geoJSONFeaturesCollection);

More here: Example implementation

TurfJS nearest function documentation

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Links to other sites can disappear or change without warning. Please edit your answer to include details on how/where to use your suggestion, and what it does. Use the link as an additional reference rather than the entire explanation. – Midavalo Mar 16 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.