Skip to content

An utility to scroll throught the features of a GeoJSON object and get the properties for further use

License

Notifications You must be signed in to change notification settings

frankbroniewski/GeoJsonScroller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Essential GeoJSON scripts part CXLVIII

Use this little class to iterate over the features of a GeoJSON object and do stuff with them. Has a .forward() and a .rewind() method, just like the good old cassette recorder. Use it with e.g. events to iterate programmatically through the features in an endless loop.

Create the GeoJsonScroller as follows: var scroller = new GeoJsonScroller(geoJsonDataObject, getDataFunction, sortFunction);

You can use notable methods on the class:

  • getData(): retrieves the data for the current feature
  • forward(): move on to the next feature and get its data. If the end is reached, continue from start
  • rewind(): go back one feature and get its data. If the start is reached, continue with the last feature in the feature's array

You need to pass a function to retrieve data from a feature:

function getData (feature) {
  var data = {},
      cols = [ "title", "desc", "image", "icon", "color", "category",
               "pitch", "bearing", "zoom", "speed", "curve" ];
  data["center"] = feature.geometry.coordinates;

  for (var key in feature.properties) {
    if (cols.indexOf(key) > -1) { data[key] = feature.properties[key]; }
  }
  return data
}

scroller = new GeoJsonScroller(myGeoJson, getData);
var data = scroller.forward();

You can pass a function in order to sort the features by some criteria. Have a look at the docs for how to write such a function.

// this works for numerical props
function sortById (a, b) {
  return a.properties.id - b.properties.id;
}

function getData (feature) {
  // bla
}

scroller = new GeoJsonScroller(myGeoJson, getData, sortById);
var data = scroller.forward();

Have fun!

About

An utility to scroll throught the features of a GeoJSON object and get the properties for further use

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published