Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rclark
Last active December 7, 2022 00:23
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rclark/5779673 to your computer and use it in GitHub Desktop.
Save rclark/5779673 to your computer and use it in GitHub Desktop.
TopoJSON-aware Leaflet layer
/*
You'll need something like this in your HTML:
<script src="http://d3js.org/topojson.v1.min.js"></script>
*/
L.TopoJSON = L.GeoJSON.extend({
addData: function(jsonData) {
if (jsonData.type === "Topology") {
for (key in jsonData.objects) {
geojson = topojson.feature(jsonData, jsonData.objects[key]);
L.GeoJSON.prototype.addData.call(this, geojson);
}
}
else {
L.GeoJSON.prototype.addData.call(this, jsonData);
}
}
});
/*
The MIT License (MIT)
Copyright (c) 2013 Ryan Clark
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
@caged
Copy link

caged commented Jun 14, 2013

Nice! I just tested something similar myself a moment ago. I like the simplicity of your implementation. Is there any particular license you're releasing this under?

@rclark
Copy link
Author

rclark commented Jun 14, 2013

Wow -- you were on this before I even knew if it worked. Well it does! And now there's more license than code -- hurray!

@kynan
Copy link

kynan commented Sep 14, 2014

Any ideas how to make this work in combination with leaflet-ajax?

@rpoddighe
Copy link

Thanks for this! You could add

return this;

to allow the same method chaining used in L.GeoJSON for full compatibility.

@bbss
Copy link

bbss commented Sep 2, 2015

Additional info with similar approach: http://blog.webkid.io/maps-with-leaflet-and-topojson/

@brendanvinson
Copy link

brendanvinson commented May 18, 2016

Forked and edited to be used the same as L.geoJson()

https://gist.github.com/brendanvinson/0e3c3c86d96863f1c33f55454705bca7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment