0
# GeoJSON Visualization
1
2
Render geographic features from GeoJSON data including countries, regions, and custom shapes with choropleth mapping capabilities and styling controls. Perfect for visualizing geographic boundaries and area-based data.
3
4
## Capabilities
5
6
### GeoJSON Layer Creation
7
8
Create layers from GeoJSON data with customizable styling for choropleth maps and geographic visualizations.
9
10
```python { .api }
11
def geojson_layer(geojson, fill_color=None, fill_opacity=0.4, stroke_color=None, stroke_opacity=1.0, stroke_weight=1.0):
12
"""
13
Create a GeoJSON layer.
14
15
Parameters:
16
- geojson (dict or list): GeoJSON data (FeatureCollection, Feature, or list of features)
17
- fill_color (str or list, optional): Fill color for polygons
18
- fill_opacity (float or list): Fill opacity (0.0-1.0)
19
- stroke_color (str or list, optional): Stroke color for outlines
20
- stroke_opacity (float or list): Stroke opacity (0.0-1.0)
21
- stroke_weight (float or list): Stroke width in pixels
22
23
Returns:
24
GeoJson: GeoJSON layer instance
25
"""
26
```
27
28
### GeoJSON Layer Widget
29
30
Container for GeoJSON features with styling and interaction capabilities.
31
32
```python { .api }
33
class GeoJson:
34
"""
35
GeoJSON layer widget for geographic feature visualization.
36
37
Attributes:
38
- features (list): List of GeoJsonFeature instances
39
"""
40
```
41
42
### Individual GeoJSON Feature
43
44
Single GeoJSON feature with customizable styling properties.
45
46
```python { .api }
47
class GeoJsonFeature:
48
"""
49
Individual GeoJSON feature widget.
50
51
Attributes:
52
- feature (dict): GeoJSON feature dictionary
53
- fill_color (str): Fill color for polygons
54
- fill_opacity (float): Fill opacity (0.0-1.0)
55
- stroke_color (str): Stroke color for outlines
56
- stroke_opacity (float): Stroke opacity (0.0-1.0)
57
- stroke_weight (float): Stroke width in pixels
58
"""
59
```
60
61
## Usage Examples
62
63
### Basic GeoJSON Layer
64
65
```python
66
import gmaps
67
import gmaps.geojson_geometries
68
69
gmaps.configure(api_key="YOUR_API_KEY")
70
71
# Load built-in geometry
72
countries = gmaps.geojson_geometries.load_geometry('countries')
73
74
# Create GeoJSON layer
75
fig = gmaps.figure()
76
geojson_layer = gmaps.geojson_layer(countries)
77
fig.add_layer(geojson_layer)
78
fig
79
```
80
81
### Styled GeoJSON Features
82
83
```python
84
import gmaps
85
import gmaps.geojson_geometries
86
87
gmaps.configure(api_key="YOUR_API_KEY")
88
89
# Load geometry data
90
us_states = gmaps.geojson_geometries.load_geometry('us-states')
91
92
# Create styled layer
93
fig = gmaps.figure()
94
geojson_layer = gmaps.geojson_layer(
95
us_states,
96
fill_color='lightblue',
97
fill_opacity=0.6,
98
stroke_color='navy',
99
stroke_weight=2.0
100
)
101
fig.add_layer(geojson_layer)
102
fig
103
```
104
105
### Choropleth Map
106
107
```python
108
import gmaps
109
import gmaps.geojson_geometries
110
import pandas as pd
111
112
gmaps.configure(api_key="YOUR_API_KEY")
113
114
# Load geometry and create sample data
115
countries = gmaps.geojson_geometries.load_geometry('countries')
116
117
# Sample population data (in practice, you'd load real data)
118
population_data = {
119
'United States': 331000000,
120
'China': 1400000000,
121
'India': 1380000000,
122
'Brazil': 213000000,
123
# ... more countries
124
}
125
126
# Create color mapping based on population
127
def get_color(population):
128
if population > 1000000000:
129
return 'darkred'
130
elif population > 500000000:
131
return 'red'
132
elif population > 100000000:
133
return 'orange'
134
elif population > 50000000:
135
return 'yellow'
136
else:
137
return 'lightgreen'
138
139
# Apply colors to features
140
colors = []
141
for feature in countries['features']:
142
country_name = feature['properties'].get('NAME', '')
143
population = population_data.get(country_name, 0)
144
colors.append(get_color(population))
145
146
fig = gmaps.figure()
147
geojson_layer = gmaps.geojson_layer(
148
countries,
149
fill_color=colors,
150
fill_opacity=0.7,
151
stroke_color='white',
152
stroke_weight=1.0
153
)
154
fig.add_layer(geojson_layer)
155
fig
156
```
157
158
### Custom GeoJSON Data
159
160
```python
161
import gmaps
162
163
gmaps.configure(api_key="YOUR_API_KEY")
164
165
# Custom GeoJSON feature
166
custom_geojson = {
167
"type": "FeatureCollection",
168
"features": [
169
{
170
"type": "Feature",
171
"geometry": {
172
"type": "Polygon",
173
"coordinates": [[
174
[-122.4194, 37.7749], # San Francisco area
175
[-122.4094, 37.7749],
176
[-122.4094, 37.7849],
177
[-122.4194, 37.7849],
178
[-122.4194, 37.7749]
179
]]
180
},
181
"properties": {
182
"name": "San Francisco Area",
183
"population": 875000
184
}
185
}
186
]
187
}
188
189
fig = gmaps.figure()
190
geojson_layer = gmaps.geojson_layer(
191
custom_geojson,
192
fill_color='purple',
193
fill_opacity=0.5,
194
stroke_color='black',
195
stroke_weight=2.0
196
)
197
fig.add_layer(geojson_layer)
198
fig
199
```
200
201
### Multiple GeoJSON Layers
202
203
```python
204
import gmaps
205
import gmaps.geojson_geometries
206
207
gmaps.configure(api_key="YOUR_API_KEY")
208
209
fig = gmaps.figure()
210
211
# Add country boundaries
212
countries = gmaps.geojson_geometries.load_geometry('countries')
213
country_layer = gmaps.geojson_layer(
214
countries,
215
fill_color='lightblue',
216
fill_opacity=0.3,
217
stroke_color='blue',
218
stroke_weight=1.0
219
)
220
fig.add_layer(country_layer)
221
222
# Add state boundaries (if available)
223
try:
224
states = gmaps.geojson_geometries.load_geometry('us-states')
225
state_layer = gmaps.geojson_layer(
226
states,
227
fill_color='none', # No fill, just borders
228
stroke_color='red',
229
stroke_weight=1.5
230
)
231
fig.add_layer(state_layer)
232
except:
233
pass # Geometry not available
234
235
fig
236
```
237
238
### Interactive GeoJSON with Hover Effects
239
240
```python
241
import gmaps
242
import gmaps.geojson_geometries
243
244
gmaps.configure(api_key="YOUR_API_KEY")
245
246
# Load geometry
247
regions = gmaps.geojson_geometries.load_geometry('world-regions')
248
249
# Create features with hover information
250
features = []
251
for feature in regions['features']:
252
# Add hover information to properties
253
feature['properties']['hover_text'] = f"Region: {feature['properties'].get('name', 'Unknown')}"
254
features.append(feature)
255
256
updated_geojson = {
257
"type": "FeatureCollection",
258
"features": features
259
}
260
261
fig = gmaps.figure()
262
geojson_layer = gmaps.geojson_layer(
263
updated_geojson,
264
fill_color='green',
265
fill_opacity=0.4,
266
stroke_color='darkgreen',
267
stroke_weight=2.0
268
)
269
fig.add_layer(geojson_layer)
270
fig
271
```