Thursday, 25 January 2018

Parsing a GeoJSON file with jQuery


I'm trying to iterate through a GeoJSON file (below) and eventually populate an array with a few attributes within "properties". I keep coming up short working through various examples I can find. Do I need to embed another $.each() to get to properties. If it isn't obvious I'm new to this and I've hit the wall.


What I have so far:


$(document).ready(function () {    
$.getJSON('testpoint.geojson', function (data) {

var items = [];
$.each(data.features, function (key, val) {
items.push('
  • ' + val + '
  • ');
    });
    $('
      ', {
      'class':'my-new-list',
      html:items.join('')
      }).appendTo('body');
      });
      });


    And my json is as follows:


    {
    "type": "FeatureCollection",
    "features": [
    {
    "type": "Feature",
    "properties": {
    "gid": 1,
    "PID": "9-18-3",

    "BCT": "BCT126",
    "OWNER_TYPE": "A",
    "LOCNO": 0,
    "LOCEXT": "",
    "STREET": "CROSBY LANE",
    "ACQUIRED": "5/7/2010",
    "GRANTOR": "John A. SPARGO",
    "UPLAND": 0,
    "WETLAND": 3.96,
    "TOTAL": 3.96,

    "HABITAT": "salt marsh"
    },
    "geometry": {
    "type": "Point",
    "coordinates": [
    -70.03209,
    41.78278
    ]
    }
    }

    ]
    }

    Answer



    You are almost there. Another .each for val.properties should work:


    $.each(data.features, function (key, val) {
    $.each(val.properties, function(i,j){
    items.push('
  • ' + j + '
  • ');
    })
    });

    No comments:

    Post a Comment

    arcpy - Changing output name when exporting data driven pages to JPG?

    Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...