0

Currently working on a project to plot addresses in Meteor using Leaflet and Data Science Toolkit (DSTK) for geocoding. Sending a curl POST request with three addresses I get the following data returned:

     {    "901 North Jackson Street, Junction City KS 66441": {
          "country_code3": "USA",
          "latitude": 39.031397,
          "country_name": "United States",
          "longitude": -96.837184,
          "street_address": "901 N Jackson St",
          "region": "KS",
          "confidence": 1.0,
          "street_number": "901",
          "locality": "Junction City",
          "street_name": "N Jackson St",
          "fips_county": "20061",
          "country_code": "US"    },    
"300 South Parkside Drive, Colorado Springs CO 80901": {
          "country_code3": "USA",
          "latitude": 38.828775,
          "country_name": "United States",
          "longitude": -104.79034,
          "street_address": "300 S Parkside Dr",
          "region": "CO",
          "confidence": 0.805,
          "street_number": "300",
          "locality": "Colorado Springs",
          "street_name": "S Parkside Dr",
          "fips_county": "08041",
          "country_code": "US"    },    
"1412 Kensington Avenue, Kansas City MO 64127": {
          "country_code3": "USA",
          "latitude": 39.095286,
          "country_name": "United States",
          "longitude": -94.530828,
          "street_address": "1412 Kensington Ave",
          "region": "MO",
          "confidence": 1.0,
          "street_number": "1412",
          "locality": "Kansas City",
          "street_name": "Kensington Ave",
          "fips_county": "29095",
          "country_code": "US"    } }

What I'm trying to do is loop through those results and insert into mongodb in Geojson format. Using the last address from above it would look like the following:

var geojsonFeature = {
"type": "Feature",
"properties": {
    "name": "1412 Kensington Avenue, Kansas City MO 64127",
    "popupContent": "1412 Kensington Avenue, Kansas City MO 64127"
},
"geometry": {
    "type": "Point",
    "coordinates": [-94.530828, 39.095286]
}

};

Any suggestions are greatly appreciated.

Godfried
  • 151
  • 1
  • 3
  • 12

1 Answers1

0

Let's say a is the object you got from your POST. It has 3 top level keys. You can iterate over these with for (var property in a){} - each of those top level keys will be returned as a string. You can then use that string to index into the entire object with bracket notation a[property] as follows:

let geojsonFeature = {};
for (var property in a){
  if ( a.hasOwnProperty(property) ){
    geojsonFeature = {
      "type": "Feature",
      "properties": {
        "name": property,
        "popupContent": property
      },
      "geometry": {
        "type": "Point",
        "coordinates": [ a[property].longitude a[property].latitude ]
      }
    };
    myGeoJsonFeatures.insert(geojsonFeature); // save geojsonFeature to your collection
  }
}
Community
  • 1
  • 1
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • Michel - Thank you so much, worked perfectly! Explanation and link also very helpful in understanding. – Godfried Sep 07 '16 at 17:17
  • So now I've run up against another issue related to the one above. If I send a curl request with three addresses to geocode and output the json data returned to the console I see all three. However, if I do an insert to mongodb it only shows the first geocoded address returned. So in the solution above if I use `myGeoJsonFeatures.insert(geojsonFeature)` only the first geocoded address gets inserted into mongo. If instead I use `console.log(geojsonFeature)` all three geocoded addresses are shown. – Godfried Sep 10 '16 at 22:11
  • I solved the issue with `wrapAsync` in the geojson curl request. – Godfried Sep 12 '16 at 00:01