gboeing / osmnx

OSMnx is a Python package to easily download, model, analyze, and visualize street networks and other geospatial features from OpenStreetMap.
https://osmnx.readthedocs.io
MIT License
4.87k stars 827 forks source link

Saved svg files are broken #19

Closed c9rbjn closed 7 years ago

c9rbjn commented 7 years ago

Hey Geoff, you did an amazing work on osmnx. But I've trouble when I want to save shapes or networks as a svg file. I've did:

import osmnx as ox
#matplotlib inline
ox.config(log_file=True, log_console=True, use_cache=True)
G = ox.graph_from_place('Hamburg, Germany', network_type='drive_service', simplify=False)
G_projected = ox.project_graph(G)
fig, ax = ox.plot_graph(G_projected, fig_height=30, node_size=0, edge_linewidth=0.5, save=True, file_format='svg', filename='Hamburg')

After running it the last line says „Saved the figure to disk in 48.39 seconds" And when I'm going to open it, Illustrator prompting an error message that the file seems to be broken. Am I doing something wrong?

I'm running osmnx on Mac OS El Capitan with Python 3.5

gboeing commented 7 years ago

Your code shouldn't work as presented because OSM's first result when geocoding "Hamburg, Germany" is a point, but OSMnx needs a polygon or multipolygon to create a network. You'd need to use the which_result argument to specify that you want the 2nd result from the geocoder. See #16.

Also, you should probably simplify your network after projecting it to reduce the number of nodes and edges to something more topologically correct for a street network.

Finally, it looks like the network for Hamburg has hundreds of thousands of nodes and edges. It would not be a surprise if Illustrator is unable to successfully open an SVG file with hundreds of thousands of elements inside it.

To figure out what's going on, first try the network for a simpler city and verify you can load it in Illustrator:

import osmnx as ox
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Modena, Italy', network_type='drive', simplify=False)
G_projected = ox.project_graph(G)
G_simplified = ox.simplify_graph(G_projected)
fig, ax = ox.plot_graph(G_simplified, fig_height=30, node_size=0, edge_linewidth=0.5, 
                        save=True, file_format='svg', filename='mod_simp')

This SVG file works just fine for me in Illustrator. Next, try it for the simplified Hamburg network (using which_result to get the correct administrative boundary geometry):

import osmnx as ox
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Hamburg, Germany', which_result=2, network_type='drive', simplify=False)
G_projected = ox.project_graph(G)
G_simplified = ox.simplify_graph(G_projected)
fig, ax = ox.plot_graph(G_simplified, fig_height=30, node_size=0, edge_linewidth=0.5, 
                        save=True, file_format='svg', filename='ham_simp')

Opening this SVG file in Illustrator makes Illustrator hang for me because of the immense number of nodes and edges, but I do not get an error message that the file is broken. If you experience the same as me, we can close this issue as it would be a limitation of Illustrator handling large SVGs with many elements. However, if you get an error message, please post its exact contents as well as full system information for your machine and your Python environment's package versions.

gboeing commented 7 years ago

@c9rbjn checking back once again on this open issue... see previous comment.

c9rbjn commented 7 years ago

Thanks Mr. Boeing for your support!