Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f082689

Browse files
lines from geopandas
1 parent abd8609 commit f082689

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

doc/python/lines-on-mapbox.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.7.7
2424
plotly:
2525
description: How to draw a line on Map in Python with Plotly.
2626
display_as: maps
@@ -57,6 +57,48 @@ fig.update_layout(mapbox_style="stamen-terrain", mapbox_zoom=4, mapbox_center_la
5757
fig.show()
5858
```
5959

60+
### Lines on Mapbox maps from GeoPandas
61+
62+
Given a GeoPandas geo-data frame with `linestring` or `multilinestring` features, one can extra point data and use `px.line_mapbox()`.
63+
64+
```python
65+
import plotly.express as px
66+
import geopandas as gpd
67+
import shapely.geometry
68+
import numpy as np
69+
import wget
70+
71+
# download a zipped shapefile
72+
wget.download("https://plotly.github.io/datasets/ne_50m_rivers_lake_centerlines.zip")
73+
74+
# open a zipped shapefile with the zip:// pseudo-protocol
75+
geo_df = gpd.read_file("zip://ne_50m_rivers_lake_centerlines.zip")
76+
77+
lats = []
78+
lons = []
79+
names = []
80+
81+
for feature, name in zip(geo_df.geometry, geo_df.name):
82+
if isinstance(feature, shapely.geometry.linestring.LineString):
83+
linestrings = [feature]
84+
elif isinstance(feature, shapely.geometry.multilinestring.MultiLineString):
85+
linestrings = feature.geoms
86+
else:
87+
continue
88+
for linestring in linestrings:
89+
x, y = linestring.xy
90+
lats = np.append(lats, y)
91+
lons = np.append(lons, x)
92+
names = np.append(names, [name]*len(y))
93+
lats = np.append(lats, None)
94+
lons = np.append(lons, None)
95+
names = np.append(names, None)
96+
97+
fig = px.line_mapbox(lat=lats, lon=lons, hover_name=names,
98+
mapbox_style="stamen-terrain", zoom=0)
99+
fig.show()
100+
```
101+
60102
### Lines on Mapbox maps using `Scattermapbox` traces
61103

62104
This example uses `go.Scattermapbox` and sets
@@ -90,4 +132,4 @@ fig.show()
90132

91133
#### Reference
92134

93-
See https://plotly.com/python/reference/scattermapbox/ for more information about mapbox and their attribute options.
135+
See https://plotly.com/python/reference/scattermapbox/ for more information about mapbox and their attribute options.

doc/python/lines-on-maps.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.4.2
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.7
2424
plotly:
2525
description: How to draw lines, great circles, and contours on maps in Python.
2626
display_as: maps
@@ -52,6 +52,47 @@ fig = px.line_geo(df, locations="iso_alpha",
5252
fig.show()
5353
```
5454

55+
### Lines on Maps from GeoPandas
56+
57+
Given a GeoPandas geo-data frame with `linestring` or `multilinestring` features, one can extra point data and use `px.line_geo()`.
58+
59+
```python
60+
import plotly.express as px
61+
import geopandas as gpd
62+
import shapely.geometry
63+
import numpy as np
64+
import wget
65+
66+
# download a zipped shapefile
67+
wget.download("https://plotly.github.io/datasets/ne_50m_rivers_lake_centerlines.zip")
68+
69+
# open a zipped shapefile with the zip:// pseudo-protocol
70+
geo_df = gpd.read_file("zip://ne_50m_rivers_lake_centerlines.zip")
71+
72+
lats = []
73+
lons = []
74+
names = []
75+
76+
for feature, name in zip(geo_df.geometry, geo_df.name):
77+
if isinstance(feature, shapely.geometry.linestring.LineString):
78+
linestrings = [feature]
79+
elif isinstance(feature, shapely.geometry.multilinestring.MultiLineString):
80+
linestrings = feature.geoms
81+
else:
82+
continue
83+
for linestring in linestrings:
84+
x, y = linestring.xy
85+
lats = np.append(lats, y)
86+
lons = np.append(lons, x)
87+
names = np.append(names, [name]*len(y))
88+
lats = np.append(lats, None)
89+
lons = np.append(lons, None)
90+
names = np.append(names, None)
91+
92+
fig = px.line_geo(lat=lats, lon=lons, hover_name=names)
93+
fig.show()
94+
```
95+
5596
## Lines on Maps with plotly.graph_objects
5697

5798
### US Flight Paths Map

0 commit comments

Comments
 (0)