Comments (8)
You need to edit the .js file, it's not JSON; specifically, you need to remove the "var statesData = " at the beginning, and then ";" at the end. Save that as "us-states.json" and then take a look at this:
http://rpubs.com/jcheng/us-states
The interactive hover-over is not possible to do with GeoJSON right now, but it will soon be possible if you use Shiny with leaflet. (As a proof of concept you can see how I previously implemented this example with the legacy jcheng5/leaflet-shiny package, here.)
An alternative is to transform the GeoJSON into sp
's SpatialPolygonDataFrame, then you can use addPolygons()
which has a popup
argument that will at least let you show "State: Density" info when a state is clicked. I made an example of this but ran into a lot of hassle because us-states.json puts Alaska's Attu Island at less than -180 degrees, which rgdal doesn't like. If you go into the GeoJSON and add 360 degrees to all of those by hand (or just remove the Attu islands), you end up with something like this:
http://rpubs.com/jcheng/us-states-2
Unfortunately the Attu islands appear at the far-right of the map, and my sp
-fu is not strong enough to move them back to -188 degrees or whatever.
from leaflet.
Thank you for detailed response. I seem to having some issues with the adjustment of the .js file to a .json file, as when I read the file it seems to get different results from you, as shown below:
> us.states <- readOGR("us-states.json", "OGRGeoJSON")
OGR data source with driver: GeoJSON
Source: "us-states.json", layer: "OGRGeoJSON"
with 1 features
It has 2 fields
Furthermore, eventually I would like to use topojson files that have been converted from .shp files as suggested here...The example uses rMaps
, but I would prefer to use rstudio's leaflet
package as it has greater flexibility...Do you have any suggestions on how to read it in so that leaflet can read the (potentially manipulated) topojson file...
Finally...is there any way to integrate the functions from your legacy jcheng5/leaflet-shiny package to get the final hover?
from leaflet.
You don't need topojson, you can just read the shp file into R and manipulate it as a SpatialPolygonsDataFrame or whatever--leaflet can handle those directly.
I'll be working on bringing back the legacy interactivity next week.
from leaflet.
Thank you for letting me know that you are going to be bringing in the legacy interactivity next week. It will make the leaflet packing pretty cool once in I think!
The reason for the topojson file is that it helps reduce the size of the files, as the .shp files can be in the region of several hundred MB, but topojson files help reduce the file size to something more manageable for web plotting.
from leaflet.
@jcheng5 👍 for adding the legacy interactivity (hover-over and mouse-click? ability to display pop-up boxes?) on custom geojson-based maps; i.e. outside of US :)
While it is possible to reduce the size of a geojson shape to 10% of original size using simplify geometry
in QGIS or equivalent in other tools, topojson goes one step further in reduction and it would be a nice to have.
from leaflet.
OK. FYI, topojson or not, if you have several hundred MB shapefiles I think you'll want to do simplification regardless--the transmission size will be smaller with topojson but you may still overwhelm the browser with so much shape data, and topojson won't help with that (since once in the browser it's basically just converted to the same objects as the geojson).
(That said, I've never actually done shapefile simplification in R. Can anyone with experience tell me how well it works, or do you have to use tools like QGIS?)
from leaflet.
@jcheng5 The code below gives what you want in R.
Playing with the tolerance will give you different degree of simplification. The tolerance 0.00001
gives values comparable to a standard simplification in QGIS, around 10% of original size.
The result should be broadly equivalent to QGIS as the underlying method should use the same Ramer–Douglas–Peucker algorithm.
library(rgdal)
library(rgeos)
fooGd <- readOGR(dsn="foo.geojson", layer="OGRGeoJSON", stringsAsFactors=FALSE)
# gSimplify strips the data portion away - it needs to be saved and re-attached
df <- fooGd@data
fooSimp <- gSimplify(fooGd, 0.00001, topologyPreserve=TRUE)
spDf <- SpatialPolygonsDataFrame(fooSimp,data=df)
#
writeOGR(spDf,'fooSimp.geojson','spDf', driver='GeoJSON',check_exists = FALSE)
A small note on topojson: geojson and topojson are different: the creation of a topojson file (using topojson from npm) reduces further the file, simply (but brilliantly) recognising that two contiguous polygons will have a side in common, hence the side only needs to be coded once.
from leaflet.
TopoJSON now supported directly: http://rstudio.github.io/leaflet/json.html
from leaflet.
Related Issues (20)
- DESCRIPTION should require minimum version for `raster` to avoid test failures HOT 8
- Problem when measuring distances in square kilometers
- Stamen Tile Deprecation Warning HOT 3
- Release leaflet 2.2.1
- Layer control fails when using clusterOptions
- Leaflet Map not loading after window size changes
- htmlwidgets 1.6.3 breaks addEasyButton HOT 1
- Update of map not works HOT 1
- addPolyline() with SpatVector fails if terra is not loaded because check_crs_terra() is missing depency terra::crs HOT 2
- toggle baseGroups legends in the same way as overlayGroups in addLayersControl HOT 1
- Proj4Leaflet-TMS.R example is broken; broken TMS in Leaflet is possibly the culprit! HOT 2
- tms = TRUE not working with known TMS provider (from NASA!) HOT 2
- Leaflet muddling RGB rasters from terra HOT 1
- No documentation on the Polyline 'offset' functionality
- Error in addLegend: unused arguments HOT 2
- addTiles does not allow display of self-hosted vector tiles
- Specifying labels as zero-length character vector causes error
- Update docs to use `{sf}` or `{terra}` HOT 1
- Remove vignettes/articles/nc after successful site build
- Broken basemaps on {pkgdown} site HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leaflet.