My goal is to get a so-called "choropleth map" (I guess) of the zip code areas in Germany. I have found the python package "folium" but it seems like it takes a .json file as input:
https://github.com/python-visualization/folium
On OpenStreetMap I only see shp.zip and .osm.pbf files. Inside the shp.zip archive I find all sorts of file endings which I have never heard of but no .json file. How do I use the data from OpenStreetMap to feed folium? Am I running into the wrong direction?
If you want to create a choropleth map you must follow these steps:
First you need a file containing info about the regions of that country. A sample .json file has been supplied with this answer, however, there are actually many file formats commonly used for maps. In your case, you need to convert your OSM shape file (.shp) into a more modern file type like .geojson. Thankfully we have ogr2ogr to do this last part:
ogr2ogr -f GeoJSON -t_srs EPSG:4326 -simplify 1000 [name].geojson [name].shp
Advice: You can also extract the administrative borders from these web sites:
* [OSM Boundaries Map 4.2][2]
* [Mapzen][3]
* [Geofabrik][4]
Download data based on it (a .csv file, for example). Obviously, the file must have a column with the ZIP Codes of that country.
Once you get these files the rest is straightforward, Follium will create the choropleth map automatically.
I wrote a simple example of this about the unemployment rate in the US:
Code:
import folium
import pandas as pd
osm = folium.Map([43, -100], zoom_start=4)
osm.choropleth(
geo_str = open('US_states.json').read(),
data = pd.read_csv("US_unemployment.csv"),
columns = ['State', 'Unemployment'],
key_on = 'feature.id',
fill_color = 'YlGn',
)
Output:
I haven't done this myself but there are various solutions for converting OSM files (.osm or .pbf) to (geo)json. For example osmtogeojson. More tools can be found at the GeoJSON page in the OSM wiki.
I went to https://overpass-turbo.eu/ (which retrieves data from openstreetmap via a specific Query Language QL) and hit run on the following code:
[timeout:900];
area[name="Deutschland"][admin_level=2][boundary=administrative]->.myarea;
rel(area.myarea)["boundary"="postal_code"];
out geom;
You can "export to geojson" but in my case that didn't work because it's too much data which cannot be processed inside the browser. But exporting the "raw data" works. So I did that and then I used "osmtogeojson" to get the right format. After that I was able to feed my openstreetmap data to folium as described in the tutorial of folium.
This answer was posted as an edit to the question Choropleth map with OpenStreetMap data by the OP user3182532 under CC BY-SA 3.0.
Related
I have been using textnets (python) to analyse a corpus. I need to export the resulting graph for further analysis / layout editing in Gephi. Having read the docs I am still confused on how to either save the resulting igraph Graph in the appropriate format or to access the pandas dataframe which could then be exported. For example using the tutorial from docs, if using:
from textnets import Corpus, Textnet
from textnets import examples
corpus = Corpus(examples.moon_landing)
tn = Textnet(corpus.tokenized(), min_docs=1)
print(tn)
I had thought I could either return a pandas data frame by calling 'tn' though this returns a 'Textnet' object.
I had also thought I could return an igraph.Graph object and then subsequently use Graph.write_gml() using something like tn.project(node_type='doc').write_gml('test.gml') to save the file in an appropriate format but this returns a ProjectedTextnet.
Any advise would be most welcome.
For the second part of your question, you can convert the textnet object to an igraph:
g = tn.graph
Then save as gml:
g.write_gml("test.gml")
I have a GeoJSON file that records a city's roads geocodes and their attributes. Sample looks like this
{"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":
{"type":"LineString","coordinates":[[lat1, long1],[lat2, long2],[lat3, long3]]},
"properties":{"ROUTE_TYPE":,"TOTAL_LANE":,"AADT":}}}}
In this case, how can I convert such JSON to a networkX graph?
Ideally a DiGraph (I do notice that direction information is not indicted in the JSON at all. I am fine with assuming all links are bidirectional.)
This question is similar to the following question on GIS stackexchange: Retrieving OpenStreetMap data with Python using GeoJSON geometry as boundary and its answer.
The main ingredients you need are the following:
geopandas to parse the geojson into a GeoDataFrame with read_file
OSMnx to transform the GeoDataFrame into a networkx.MultiDiGraph using osmnx.utils_graph.graph_from_gdfs
Alternatively, you could use ignore_geometry=Truein the first step and use nx.from_pandas_edgelist
I am trying to display the following geojson file in a folium map in Python but it just shows an empty map with none of the data.
Here are the steps I have tried:
I tried using the python code below but nothing shows up.
I tried other geojson files in the github repository below using the same code and the data show up without any issue, so it looks like my python code is fine
I opened the "census_tracts_2010.geojson" file in github and Mapshaper, the data showed up perfectly without any issue, so it doesn't look like the geojson file is corrupted
Could anyone please let me know how I can fix it?
Geojson file:
https://github.com/dwillis/nyc-maps/blob/master/census_tracts_2010.geojson
Python code:
import folium
m = folium.Map(location=[40.66393072,-73.93827499], zoom_start=13)
m.choropleth(geo_path="census_tracts_2010.geojson")
m.save(outfile='datamap.html')
Thanks a lot!
That file is not a GeoJson it is a TopoJson. You need to use folium.TopoJson instead.
import folium
m = folium.Map(location=[40.66393072,-73.93827499], zoom_start=13)
folium.TopoJson(
open('census_tracts_2010.geojson'),
object_path='objects.nyct2010',
).add_to(m)
m
You need to open the geojson file.
m.choropleth(open("census_tracts_2010.geojson"))
Take a look at the examples https://folium.readthedocs.io/en/latest/quickstart.html
Try this: m.add_child(folium.GeoJson(data = open("census_tracts_2010.geojson"))) and then call m.save() fun
I have a stack of CT-scan images. After processing (one image from those stack) CT-scan image using Matlab, I saved XY coordinates for each different boundary region in different Excel sheets as follows:
I = imread('myCTscan.jpeg');
BW = im2bw(I);
[coords, labeledImg] = bwboundaries(BW, 4, 'holes');
sheet = 1;
for n=1:length(coords);
xlswrite('fig.xlsx',coords{n,1},sheet,'A1');
sheet = sheet+1;
end
The next step is then to import this set of coordinates and plot it into Abaqus CAE Sketch for finite element analysis.
I figure out that my workflow is something like this:
Import Excel workbook
For each sheet in workbook:
2.1. For each row: read both column to get xy coordinates (each row has two column, x and y coordinate)
2.2. Put each xy coordinates inside a list
2.3. From list, sketch using spline method
Repeat step 2 for other sheets within the workbook
I searched for a while and found something like this:
from abaqus import *
lines= open('fig.xlsx', 'r').readlines()
pointList= []
for line in lines:
pointList.append(eval('(%s)' %line.strip()))
s1= mdb.models['Model-1'].ConstrainedSketch(name='mySketch', sheetSize=500.0)
s1.Spline(points= pointList)
But this only read XY coordinates from only one sheet and I'm stuck at step 3 above. Thus my problem is that how to read these coordinates in different sheets using Abaqus/Python (Abaqus 6.14, Python 2.7) script?
I'm new to Python programming, I can read and understand the syntax but can't write very well (I'm still struggling on how to import Python module in Abaqus). Manually type each coordinates (like in Abaqus' modelAExample.py tutorial) is practically impossible since each of my CT-scan image can have 100++ of boundary regions and 10k++ points.
I'm using:
Windows 7 x64
Abaqus 6.14 (with built in Python 2.7)
Excel 2013
Matlab 2016a with Image Processing Toolbox
You are attempting to read excel files as comma separated files. CSV files by definition can not have more than one tab. Your read command is interpreting the file as a csv and not allowing you to iterate over the tabs in your file (though it begs the question how your file is opening properly in the first place as you are saving an xlsx and reading a csv).
There are numerous python libraries that will parse and process XLS/XLSX files.
Take a look at pyxl and use it to read your file in.
You would likely use something like
from openpyxl import Workbook
(some commands to open the workbook)
listofnames=wb.sheetnames
for k in listofnames:
ws=wb.worksheets(k)
and then input your remaining commands.
I want to import a geojson file into python so I can map it with a visualization package vincent and merge with other data in a pandas data frame.
To be specific, the said geojson file is: http://ec2-54-235-58-226.compute-1.amazonaws.com/storage/f/2013-05-12T03%3A50%3A18.251Z/dcneighorhoodboundarieswapo.geojson. It's a map of DC with neighborhoods, put together by Justin Grimes.
Right now, I'm just trying to visualize this map on notebook. Here's my code:
import vincent
map=r'http://ec2-54-235-58-226.compute-1.amazonaws.com/storage/f/2013-05-12T03%3A50%3A18.251Z/dcneighorhoodboundarieswapo.geojson'
geo_data = [{'name': 'countries',
'url': map,
'feature': "features"}]
vis = vincent.Map(geo_data=geo_data, scale=5000)
vis
but I keep getting an error message, local host says: [Vega err] loading failed.
What am I doing wrong here?
I don't yet know much about GIS and Python so I ask you be specific in your explanation. Thank you in advance.
At this moment you can't use for you maps with vincent anything but topojson file format (see https://github.com/mbostock/topojson/wiki).
You can convert geojson into topojson using web tools like https://mapshaper.org/ or using command-line utility (https://github.com/mbostock/topojson/wiki/Command-Line-Reference) with command like this:
topojson -p -o <target-file>.topo.json -- <input-file>.json
(-p says utility to keep properties of geometries.)