icechart module¶
Main module for icechart package by Naheem Adebisi.
Map
¶
This Map class inherits the ipyleaflet Map Class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ipyleaflet |
ipyleaflet.Map |
An ipyleaflet map. |
required |
add_geojson(self, in_geojson, style=None, layer_name='Untitled')
¶
Adds a GeoJSON file to the map.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_geojson |
str |
The file path to the input GeoJSON. |
required |
style |
dict |
The style for the GeoJSON layer. Defaults to None. |
None |
layer_name |
str |
The layer name for the GeoJSON layer. Defaults to "Untitled". |
'Untitled' |
Exceptions:
Type | Description |
---|---|
FileNotFoundError |
If the provided file path does not exist. |
TypeError |
If the input geojson is not a str or dict. |
Source code in icechart/icechart.py
def add_geojson(self, in_geojson, style=None, layer_name="Untitled"):
"""Adds a GeoJSON file to the map.
Args:
in_geojson (str): The file path to the input GeoJSON.
style (dict, optional): The style for the GeoJSON layer. Defaults to None.
layer_name (str, optional): The layer name for the GeoJSON layer. Defaults to "Untitled".
Raises:
FileNotFoundError: If the provided file path does not exist.
TypeError: If the input geojson is not a str or dict.
"""
import json
if layer_name == "Untitled":
layer_name = "Untitled " + random_string()
if isinstance(in_geojson, str):
if not os.path.exists(in_geojson):
raise FileNotFoundError("The provided GeoJSON file could not be found.")
with open(in_geojson) as f:
data = json.load(f)
elif isinstance(in_geojson, dict):
data = in_geojson
else:
raise TypeError("The input geojson must be a type of str or dict.")
if style is None:
style = {
"stroke": True,
"color": "#000000",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.4,
}
geo_json = ipyleaflet.GeoJSON(data=data, style=style, name=layer_name)
self.add_layer(geo_json)
add_shapefile(self, in_shp, style=None, layer_name='Untitled')
¶
add a shapefile layer to the Map
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_shp |
str |
file path to the input shapefile |
required |
style |
dict |
The style dictionary. Defaults to None. |
None |
layer_name |
str |
The layer name for the shapefile. Defaults to "Untitled". |
'Untitled' |
Source code in icechart/icechart.py
def add_shapefile(self, in_shp, style=None, layer_name="Untitled"):
"""add a shapefile layer to the Map
Args:
in_shp (str): file path to the input shapefile
style (dict, optional): The style dictionary. Defaults to None.
layer_name (str, optional): The layer name for the shapefile. Defaults to "Untitled".
"""
geojson = shp_to_geoson(in_shp)
self.add_geojson(geojson, style=style, layer_name=layer_name)
shp_to_geoson(in_shp, out_geojson=None)
¶
Converts a shapefile to GeoJSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_shp |
str |
The file path to the input shapefile. |
required |
out_geojson |
str |
The file path to the output GeoJSON. Defaults to None. |
None |
Exceptions:
Type | Description |
---|---|
FileNotFoundError |
If the input shapefile does not exist. |
Returns:
Type | Description |
---|---|
dict |
The dictionary of the GeoJSON. |
Source code in icechart/icechart.py
def shp_to_geoson(in_shp, out_geojson=None):
"""Converts a shapefile to GeoJSON.
Args:
in_shp (str): The file path to the input shapefile.
out_geojson (str, optional): The file path to the output GeoJSON. Defaults to None.
Raises:
FileNotFoundError: If the input shapefile does not exist.
Returns:
dict: The dictionary of the GeoJSON.
"""
import json
import shapefile
in_shp = os.path.abspath(in_shp)
if not os.path.exists(in_shp):
raise FileNotFoundError("The provided shapefile could not be found.")
sf = shapefile.Reader(in_shp)
geojson = sf.__geo_interface__
if out_geojson is None:
return geojson
else:
out_geojson = os.path.abspath(out_geojson)
out_dir = os.path.dirname(out_geojson)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
with open(out_geojson, "w") as f:
f.write(json.dumps(geojson))