leaflet-dvf
leaflet-dvf copied to clipboard
Creating compatible js files for choropleth mapping
Is there some documentation available for the structure used by countryData.js and stateData.js?
I would like to use other GeoJSON files, such as U.S. counties or legislative districts, but I'm not sure how the GeoJSON is being transformed, exactly, so I'm having trouble making my own files.
In the docs, I see that
Polygons are indexed via a state or country code and lookups are created to map various state/country code formats to the default index format.
That use of keys seems to create invalid GeoJSON per spec, therefore, it will be difficult to create additional files without a conversion script. Is such a script available, by any chance?
Good question. I know the documentation needs some work, and now I know of at least one area where I need to devote some time to documenting.
The basic structure is a JavaScript object key/value lookup. Each key is a code (e.g. county code), and each value is the GeoJSON representing the polygon associated with that code. This makes it efficient to find the polygon or other geometry associated with a given code. If you have GeoJSON you want to use for polygons, there's no need to create your own key/value lookup. There's built in support in L.DataLayer based classes (e.g. L.ChoroplethDataLayer) for this particular use case - where you have GeoJSON representing a set of polygons or other features that you want to map and some input data that has properties that you want to use to style those polygons/features. Check out the Netherlands Population by ZIP 2 example: http://humangeo.github.io/leaflet-dvf/examples/html/nlzip.html.
In this case, the key things to note in the DataLayer options are:
- locationLookup: zipData, (A GeoJSON FeatureCollection representing polygons/features to be styled - in your case, this could be a FeatureCollection of county boundaries with FIPS county code property)
- locationMode: L.LocationModes.LOOKUP (this tells the DataLayer class that it needs to lookup the geometry from the object identified in locationLookup - the codeField property (below) will be used as the index in the key/value lookup. NOTE: this requires that each GeoJSON feature in the FeatureCollection has the property with the same name identified in codeField (e.g. county FIPS code))
- codeField: 'GEOZIP2NL' (this tells the DataLayer class that the input dataset has a property called GEOZIP2NL that will be used to lookup associated polygons/features - in your case, this might be a FIPS county code property)
- locationTextField: 'GEOZIP2NL' (this tells the DataLayer that the value of this property in the locationLookup GeoJSON will be used for display purposes - in your case, this might be the county name)
As an alternative to this approach, if you want to create your own custom lookup from a GeoJSON FeatureCollection, there's also the L.GeometryUtils.indexFeatureCollection method that takes a GeoJSON FeatureCollection and a property key as input and returns a key/value lookup of the given property mapped to its associated polygon. This lookup can then be used in a DataLayer with the L.LocationModes.CUSTOM locationMode.
Hope this helps. Does this answer your question? Let me know if you have any additional questions.
On Tuesday, December 3, 2013, Martin Burch wrote:
Is there some documentation available for the structure used by countryData.js and stateData.js?
I would like to use other GeoJSON files, such as U.S. counties or legislative districts, but I'm not sure how the GeoJSON is being transformed, exactly, so I'm having trouble making my own files.
In the docs, I see that
Polygons are indexed via a state or country code and lookups are created to map various state/country code formats to the default index format.
That use of keys seems to create invalid GeoJSON per spec, therefore, it will be difficult to create additional files without a conversion script.
— Reply to this email directly or view it on GitHubhttps://github.com/humangeo/leaflet-dvf/issues/8 .
I just pushed an update that has a US county example (still a work in progress) that might be helpful.