0

I want to add data from a dataframe to a geojson File. The geo json file looks like this:

"properties": {"name_2": "Freiburg im Breisgau", "name_0": "Germany", "name_1": "Baden-W\u00fcrttemberg", "type_2": "Stadtkreis", "temp": 300, "engtype_2": "District", "geo_point_2d": [47.9925229956, 7.81807596197], "hasc_2": "DE.BW.FB", "id_2": 12, "cca_2": "08311", "id_0": 86, "id_1": 1, "iso": "DEU", "ccn_2": 0}},{"type": "Feature"}}

the dataframe would look like this enter image description here

lat and long would be optional. Would be great if i could map the temp data to the geojson by comparing the name of the city. (city in Excel Data and name_2 in the geojson data.

1337_OC
  • 13
  • 3
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly did you try and where are you getting stuck? – MrFlick Feb 18 '20 at 21:39
  • Thanks for the comment @MrFlick i have edited my post, hope people can understand it better now – 1337_OC Feb 19 '20 at 20:25
  • All the plotting code in your question is unnecessary. What you're actually asking is how do you append / merge data from Excel (I assume a data.frame structure) onto a GeoJSON file, correct? If so, you should provide a small GeoJSON example and a small data.frame in your question, to replicate the issue you're having – SymbolixAU Feb 19 '20 at 21:35
  • and for the GeoJSON example, it has to be valid & complete so other people can copy & paste and use it directly. The example you've given isn't valid (it has the trailing `...` at the end and isn't correctly closed by brackets). – SymbolixAU Feb 19 '20 at 21:45
  • The complete geojson file can be found at the link in the second sentence of my post. I want to merge data from Excel or from web scraping to the geojson data. This data would countain the Cityname and the temprature in °C, and maybe i have to resolve the location or add it manualy to the data. So it would bei lng, lat, temp and cityname. – 1337_OC Feb 19 '20 at 22:23
  • I understand what you want to do, but without a **minimal** and **reproducible** example (of both the geojson and the Excel data) it's hard to help. Where minimal means not having to download a whole geojson file from an external link (your question should be self-contained) and a maximum of 10 lines of data. – SymbolixAU Feb 19 '20 at 22:42
  • okay i understand, thanks, i will edit my question – 1337_OC Feb 19 '20 at 22:48
  • the GeoJSON is still not valid. But you'll want to do something along these lines - `sf <- sf::st_read("your_geojson.json"); df <- readxl::read_excel("your.xls"); sf <- merge( x = sf, y = df, by.x = "name_2", by.y = "city")` – SymbolixAU Feb 19 '20 at 23:04
  • thank's so far. i now have the dataframe "sf" with all the data i need. But now i am not able to plott it in leaflet anymore with my existing code. i thinks thats becouse its now a simple dataframe in form of a table and the loaded dataframe i used befor directly from the json is a "Large SpatialPilygonesDataFrame". :/ – 1337_OC Feb 19 '20 at 23:56
  • yes what you have now is an `sf` object, it supersedes `sp` (SpaitalPolygonsDataFrame). There are numerous examples of how to plot `sf` objects with leafet. And given you have a mapbox token, you can also try [mapdeck](https://symbolixau.github.io/mapdeck/articles/layers.html) – SymbolixAU Feb 19 '20 at 23:58
  • Or you can convert it back to `sp` using `as(sf, "Spatial")` – SymbolixAU Feb 20 '20 at 00:06

0 Answers0