26 kepler gl
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import leafmap.kepler as leafmap
import leafmap.kepler as leafmap
Create an interactive map. You can specify various parameters to initialize the map, such as center
, zoom
, height
, and widescreen
.
In [3]:
Copied!
m = leafmap.Map(center=[40, -100], zoom=2, height=600, widescreen=False)
m
m = leafmap.Map(center=[40, -100], zoom=2, height=600, widescreen=False)
m
Save the map to an interactive html. To hide the side panel and disable map customization. Set read_only=False
In [4]:
Copied!
m.to_html(outfile="../html/kepler.html", read_only=False)
m.to_html(outfile="../html/kepler.html", read_only=False)
Display the interactive map in a notebook cell.
In [5]:
Copied!
# m.static_map(width=950, height=600, read_only=True)
# m.static_map(width=950, height=600, read_only=True)
Add a CSV to the map. If you have a map config file, you can directly apply config to the map.
In [6]:
Copied!
m = leafmap.Map(center=[37.7621, -122.4143], zoom=12)
in_csv = (
'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/hex_data.csv'
)
config = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/hex_config.json'
m.add_csv(in_csv, layer_name="hex_data", config=config)
m
m = leafmap.Map(center=[37.7621, -122.4143], zoom=12)
in_csv = (
'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/hex_data.csv'
)
config = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/hex_config.json'
m.add_csv(in_csv, layer_name="hex_data", config=config)
m
Save the map configuration as a JSON file.
In [7]:
Copied!
m.save_config("cache/config.json")
m.save_config("cache/config.json")
Save the map to an interactive html.
In [8]:
Copied!
m.to_html(outfile="../html/kepler_hex.html")
m.to_html(outfile="../html/kepler_hex.html")
Add a GeoJSON to the map.
In [9]:
Copied!
m = leafmap.Map(center=[20, 0], zoom=1)
lines = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson'
m.add_geojson(lines, layer_name="Cable lines")
m
m = leafmap.Map(center=[20, 0], zoom=1)
lines = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson'
m.add_geojson(lines, layer_name="Cable lines")
m
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/requests/models.py:910, in Response.json(self, **kwargs) 909 try: --> 910 return complexjson.loads(self.text, **kwargs) 911 except JSONDecodeError as e: 912 # Catch JSON-related errors and raise as requests.JSONDecodeError 913 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File ~/.local/lib/python3.9/site-packages/simplejson/__init__.py:525, in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw) 521 if (cls is None and encoding is None and object_hook is None and 522 parse_int is None and parse_float is None and 523 parse_constant is None and object_pairs_hook is None 524 and not use_decimal and not kw): --> 525 return _default_decoder.decode(s) 526 if cls is None: File ~/.local/lib/python3.9/site-packages/simplejson/decoder.py:373, in JSONDecoder.decode(self, s, _w, _PY3) 372 if end != len(s): --> 373 raise JSONDecodeError("Extra data", s, end, len(s)) 374 return obj JSONDecodeError: Extra data: line 1 column 4 - line 1 column 15 (char 3 - 14) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/leafmap/kepler.py:134, in Map.add_geojson(self, in_geojson, layer_name, config, **kwargs) 133 if in_geojson.startswith("http"): --> 134 data = requests.get(in_geojson).json() 135 else: File ~/.local/lib/python3.9/site-packages/requests/models.py:917, in Response.json(self, **kwargs) 916 else: --> 917 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: [Errno Extra data] 404: Not Found: 3 During handling of the above exception, another exception occurred: Exception Traceback (most recent call last) Input In [9], in <cell line: 3>() 1 m = leafmap.Map(center=[20, 0], zoom=1) 2 lines = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson' ----> 3 m.add_geojson(lines, layer_name="Cable lines") 4 m File ~/.local/lib/python3.9/site-packages/leafmap/kepler.py:149, in Map.add_geojson(self, in_geojson, layer_name, config, **kwargs) 147 raise TypeError("The input geojson must be a type of str or dict.") 148 except Exception as e: --> 149 raise Exception(e) 151 self.add_data(data, name=layer_name) 152 self.load_config(config) Exception: [Errno Extra data] 404: Not Found: 3
In [10]:
Copied!
m.to_html("../html/kepler_lines.html")
m.to_html("../html/kepler_lines.html")
Add a GeoJSON with US state boundaries to the map.
In [11]:
Copied!
m = leafmap.Map(center=[50, -110], zoom=2)
polygons = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/us-states.json'
m.add_geojson(polygons, layer_name="Countries")
m
m = leafmap.Map(center=[50, -110], zoom=2)
polygons = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/us-states.json'
m.add_geojson(polygons, layer_name="Countries")
m
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/requests/models.py:910, in Response.json(self, **kwargs) 909 try: --> 910 return complexjson.loads(self.text, **kwargs) 911 except JSONDecodeError as e: 912 # Catch JSON-related errors and raise as requests.JSONDecodeError 913 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File ~/.local/lib/python3.9/site-packages/simplejson/__init__.py:525, in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw) 521 if (cls is None and encoding is None and object_hook is None and 522 parse_int is None and parse_float is None and 523 parse_constant is None and object_pairs_hook is None 524 and not use_decimal and not kw): --> 525 return _default_decoder.decode(s) 526 if cls is None: File ~/.local/lib/python3.9/site-packages/simplejson/decoder.py:373, in JSONDecoder.decode(self, s, _w, _PY3) 372 if end != len(s): --> 373 raise JSONDecodeError("Extra data", s, end, len(s)) 374 return obj JSONDecodeError: Extra data: line 1 column 4 - line 1 column 15 (char 3 - 14) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) File ~/.local/lib/python3.9/site-packages/leafmap/kepler.py:134, in Map.add_geojson(self, in_geojson, layer_name, config, **kwargs) 133 if in_geojson.startswith("http"): --> 134 data = requests.get(in_geojson).json() 135 else: File ~/.local/lib/python3.9/site-packages/requests/models.py:917, in Response.json(self, **kwargs) 916 else: --> 917 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: [Errno Extra data] 404: Not Found: 3 During handling of the above exception, another exception occurred: Exception Traceback (most recent call last) Input In [11], in <cell line: 3>() 1 m = leafmap.Map(center=[50, -110], zoom=2) 2 polygons = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/us-states.json' ----> 3 m.add_geojson(polygons, layer_name="Countries") 4 m File ~/.local/lib/python3.9/site-packages/leafmap/kepler.py:149, in Map.add_geojson(self, in_geojson, layer_name, config, **kwargs) 147 raise TypeError("The input geojson must be a type of str or dict.") 148 except Exception as e: --> 149 raise Exception(e) 151 self.add_data(data, name=layer_name) 152 self.load_config(config) Exception: [Errno Extra data] 404: Not Found: 3
In [12]:
Copied!
m.to_html("../html/kepler_states.html")
m.to_html("../html/kepler_states.html")
Add a shapefile to the map.
In [13]:
Copied!
m = leafmap.Map(center=[20, 0], zoom=1)
in_shp = "https://github.com/giswqs/leafmap/raw/master/examples/data/countries.zip"
m.add_shp(in_shp, "Countries")
m
m = leafmap.Map(center=[20, 0], zoom=1)
in_shp = "https://github.com/giswqs/leafmap/raw/master/examples/data/countries.zip"
m.add_shp(in_shp, "Countries")
m
In [14]:
Copied!
m.to_html("../html/kepler_countries.html")
m.to_html("../html/kepler_countries.html")
Add a GeoPandas GeoDataFrame to the map.
In [15]:
Copied!
import geopandas as gpd
import geopandas as gpd
In [16]:
Copied!
gdf = gpd.read_file(
"https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/world_cities.geojson"
)
gdf = gpd.read_file(
"https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/world_cities.geojson"
)
In [17]:
Copied!
gdf
gdf
Out[17]:
name | sov_a3 | latitude | longitude | pop_max | geometry | |
---|---|---|---|---|---|---|
0 | Bombo | UGA | 0.58330 | 32.53330 | 75000 | POINT (32.53330 0.58330) |
1 | Fort Portal | UGA | 0.67100 | 30.27500 | 42670 | POINT (30.27500 0.67100) |
2 | Potenza | ITA | 40.64200 | 15.79900 | 69060 | POINT (15.79900 40.64200) |
3 | Campobasso | ITA | 41.56300 | 14.65600 | 50762 | POINT (14.65600 41.56300) |
4 | Aosta | ITA | 45.73700 | 7.31500 | 34062 | POINT (7.31500 45.73700) |
... | ... | ... | ... | ... | ... | ... |
1244 | Rio de Janeiro | BRA | -22.92502 | -43.22502 | 11748000 | POINT (-43.22502 -22.92502) |
1245 | Sao Paulo | BRA | -23.55868 | -46.62502 | 18845000 | POINT (-46.62502 -23.55868) |
1246 | Sydney | AUS | -33.92001 | 151.18518 | 4630000 | POINT (151.18518 -33.92001) |
1247 | Singapore | SGP | 1.29303 | 103.85582 | 5183700 | POINT (103.85582 1.29303) |
1248 | Hong Kong | CHN | 22.30498 | 114.18501 | 7206000 | POINT (114.18501 22.30498) |
1249 rows × 6 columns
In [18]:
Copied!
m = leafmap.Map(center=[20, 0], zoom=1)
m.add_gdf(gdf, "World cities")
m
m = leafmap.Map(center=[20, 0], zoom=1)
m.add_gdf(gdf, "World cities")
m
In [19]:
Copied!
m.to_html("../html/kepler_cities.html")
m.to_html("../html/kepler_cities.html")
Last update:
2022-03-14