Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/addtyping #494

Merged
merged 49 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
6083d96
first init
krishnaglodha Jul 15, 2023
fc2be1d
added typing for map
krishnaglodha Jul 15, 2023
64df97f
added colormaps
krishnaglodha Jul 16, 2023
dcb8364
working on common
krishnaglodha Jul 16, 2023
87ca1a6
fix spell error
krishnaglodha Jul 16, 2023
3f433b5
added deck
krishnaglodha Jul 16, 2023
afc49d2
working on folium
krishnaglodha Jul 16, 2023
addaae4
worked on folium map
krishnaglodha Jul 17, 2023
cc1b3d5
fixed common
krishnaglodha Jul 17, 2023
f78e85b
added heremap
krishnaglodha Jul 17, 2023
0763cc8
added kepler
krishnaglodha Jul 17, 2023
3c459ee
added osm
krishnaglodha Jul 17, 2023
4cf157b
updated pc
krishnaglodha Jul 17, 2023
661b9e7
changed Tuple to tuple
krishnaglodha Jul 17, 2023
43d03d8
added plotlymap
krishnaglodha Jul 17, 2023
4598bc3
working on stack
krishnaglodha Jul 17, 2023
5ad9fcc
updated osm.py and req
krishnaglodha Jul 18, 2023
ffaea01
updated stac
krishnaglodha Jul 18, 2023
fd43fbf
updated all py files
krishnaglodha Jul 18, 2023
2481732
removed stac_io type hint
krishnaglodha Jul 18, 2023
c5e670e
tuple to Tuple
krishnaglodha Jul 18, 2023
52ae83d
tuple to Tuple
krishnaglodha Jul 18, 2023
f6184f3
fixed typos
krishnaglodha Jul 18, 2023
1172614
updated bokehmap to Sequency
krishnaglodha Jul 19, 2023
5ccb401
updated stac and plotlymap
krishnaglodha Jul 19, 2023
4b80a57
updated plotlymap
krishnaglodha Jul 19, 2023
dca221b
access_token - None
krishnaglodha Jul 19, 2023
c16c880
updated with black
krishnaglodha Jul 19, 2023
76bf155
removed kml
krishnaglodha Jul 19, 2023
249a888
updated basemap and stac
krishnaglodha Jul 19, 2023
bd45c1e
updated bokeh
krishnaglodha Jul 19, 2023
d586515
Merge branch 'master' into fix/addtyping
krishnaglodha Jul 19, 2023
b47efef
updated notebook
krishnaglodha Jul 19, 2023
e962c5e
Merge branch 'fix/addtyping' of https://github.com/krishnaglodha/leaf…
krishnaglodha Jul 19, 2023
147552a
Update mkdocs.yml
krishnaglodha Jul 19, 2023
35b010e
updated req
krishnaglodha Jul 19, 2023
4ba8314
updated mkdocs
krishnaglodha Jul 19, 2023
de756ef
removed geemap
krishnaglodha Jul 20, 2023
f6c1a45
made collection as Optional
krishnaglodha Jul 20, 2023
c1a666a
added None to Collection
krishnaglodha Jul 21, 2023
6e43faf
Fix bokeh error
giswqs Aug 8, 2023
1794bf4
Fix bokehmap typing
giswqs Aug 8, 2023
cea8e8b
Pin pyymal version
giswqs Aug 8, 2023
fc6eaaf
Fix pyymal error
giswqs Aug 8, 2023
61d6a19
Skip notebook 71
giswqs Aug 8, 2023
e6bf16b
Fix button width error
giswqs Aug 8, 2023
e0ecdf5
Fix basemap notebook error
giswqs Aug 9, 2023
1224dcc
Improve typing
giswqs Aug 9, 2023
6ba14b8
Improve typing
giswqs Aug 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
gdalinfo --version
- name: Install dependencies
run: |
pip install --no-cache-dir Cython
pip install --no-cache-dir Cython PyYAML==6.0.1
pip install -r requirements.txt -r requirements_dev.txt
pip install .
- name: Discover typos with codespell
Expand Down
16 changes: 8 additions & 8 deletions docs/notebooks/02_using_basemaps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import xyzservices.providers as xyz"
"# import os\n",
"# import xyzservices.providers as xyz"
]
},
{
Expand All @@ -191,8 +191,8 @@
"metadata": {},
"outputs": [],
"source": [
"basemap = xyz.HEREv3.basicMap\n",
"basemap"
"# basemap = xyz.HEREv3.basicMap\n",
"# basemap"
]
},
{
Expand All @@ -208,7 +208,7 @@
"metadata": {},
"outputs": [],
"source": [
"basemap['apiKey'] = os.environ[\"HEREMAPS_API_KEY\"]"
"# basemap['apiKey'] = os.environ[\"HEREMAPS_API_KEY\"]"
]
},
{
Expand All @@ -224,9 +224,9 @@
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
"m.add_basemap(basemap)\n",
"m"
"# m = leafmap.Map()\n",
"# m.add_basemap(basemap)\n",
"# m"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/38_plotly.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"metadata": {},
"outputs": [],
"source": [
"# leafmap.fix_widget_error()"
"leafmap.fix_widget_error()"
]
},
{
Expand Down
19 changes: 10 additions & 9 deletions leafmap/basemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ipyleaflet
import xyzservices.providers as xyz
from .common import check_package, planet_tiles
from typing import Dict, Optional, List

# from box import Box

Expand Down Expand Up @@ -228,7 +229,7 @@ def _unpack_sub_parameters(var, param):
return temp


def get_xyz_dict(free_only=True, france=False):
def get_xyz_dict(free_only: bool = True, france: bool = False) -> Dict:
"""Returns a dictionary of xyz services.

Args:
Expand Down Expand Up @@ -279,7 +280,7 @@ def get_xyz_dict(free_only=True, france=False):
return xyz_dict


def xyz_to_leaflet():
def xyz_to_leaflet() -> Dict:
"""Convert xyz tile services to ipyleaflet tile layers.

Returns:
Expand All @@ -306,7 +307,7 @@ def xyz_to_leaflet():
return leaflet_dict


def xyz_to_pydeck():
def xyz_to_pydeck() -> Dict:
"""Convert xyz tile services to pydeck custom tile layers.

Returns:
Expand Down Expand Up @@ -345,7 +346,7 @@ def xyz_to_pydeck():
return pydeck_dict


def xyz_to_folium():
def xyz_to_folium() -> Dict:
"""Convert xyz tile services to folium tile layers.

Returns:
Expand Down Expand Up @@ -409,7 +410,7 @@ def xyz_to_folium():
return folium_dict


def xyz_to_heremap():
def xyz_to_heremap() -> Dict:
"""Convert xyz tile services to hermap tile layers.

Returns:
Expand Down Expand Up @@ -510,7 +511,7 @@ def xyz_to_heremap():
return heremap_dict


def xyz_to_plotly():
def xyz_to_plotly() -> Dict:
"""Convert xyz tile services to plotly tile layers.

Returns:
Expand Down Expand Up @@ -546,7 +547,7 @@ def xyz_to_plotly():
return plotly_dict


def xyz_to_bokeh():
def xyz_to_bokeh() -> Dict:
"""Convert xyz tile services to bokeh tile layers.

Returns:
Expand Down Expand Up @@ -600,13 +601,13 @@ def search_qms(keywords, limit=10):
return services["results"][:limit]


def get_qms(service_id):
def get_qms(service_id: str) -> Dict:
QMS_API = "https://qms.nextgis.com/api/v1/geoservices"
service_details = requests.get(f"{QMS_API}/{service_id}")
return service_details.json()


def qms_to_leafmap(service_id):
def qms_to_leafmap(service_id: str) -> Dict:
service_details = get_qms(service_id)
name = service_details["name"]
url = service_details["url"]
Expand Down
128 changes: 72 additions & 56 deletions leafmap/bokehmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bokeh.io import output_notebook
from .basemaps import xyz_to_bokeh
from .common import *
from typing import Optional, List, Sequence, Tuple, Dict

os.environ["OUTPUT_NOTEBOOK"] = "False"
basemaps = Box(xyz_to_bokeh(), frozen_box=True)
Expand All @@ -14,13 +15,13 @@
class Map:
def __init__(
self,
center=[10, 0],
zoom=2,
width=800,
height=400,
basemap="OpenStreetMap",
grid_visible=False,
output_notebook=True,
center: List[float] = [10, 0],
zoom: float = 2,
width: float = 800,
height: float = 400,
basemap: Optional[str] = "OpenStreetMap",
grid_visible: bool = False,
output_notebook: bool = True,
**kwargs,
):
if "x_axis_type" not in kwargs:
Expand Down Expand Up @@ -67,7 +68,12 @@ def _repr_mimebundle_(self, **kwargs):
os.environ["OUTPUT_NOTEBOOK"] = "True"
show(self.figure)

def add_basemap(self, basemap="OpenStreetMap", retina=True, **kwargs):
def add_basemap(
self,
basemap: Optional[str] = "OpenStreetMap",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that we don't want the typing to raise an error on cases like:

map.add_basemap(None, None)

In these cases, the defaults would not be used. Nones would be
used in the function body. There are a lot of cases like this in the PR where None is allowed, but would cause a runtime exception

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these Optional[T]s should be replaced by T? Signaling to the user that None is not a valid parameter, avoiding some runtime exceptions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can improve the function body to raise errors when basemap is None. Indicating this error in the typing would also be helpful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep at Optional[str] and then handle None in code itself ?

retina: Optional[bool] = True,
**kwargs,
):
"""Adds a basemap to the map.

Args:
Expand Down Expand Up @@ -104,8 +110,10 @@ def add_basemap(self, basemap="OpenStreetMap", retina=True, **kwargs):
raise ValueError(
f"Basemap {basemap} is not supported. Please choose one from {basemaps.keys()}"
)
elif basemap is None:
raise ValueError("Please specify a valid basemap")

def add_tile(self, tile, **kwargs):
def add_tile(self, tile: str, **kwargs):
"""Adds a tile to the map.

Args:
Expand All @@ -121,12 +129,12 @@ def add_tile(self, tile, **kwargs):

def add_cog_layer(
self,
url,
attribution="",
bands=None,
titiler_endpoint=None,
cog_args={},
fit_bounds=True,
url: str,
attribution: str = "",
bands: Optional[List[str]] = None,
titiler_endpoint: Optional[str] = None,
cog_args: Dict = {},
fit_bounds: bool = True,
**kwargs,
):
"""Adds a COG TileLayer to the map.
Expand Down Expand Up @@ -155,15 +163,15 @@ def add_cog_layer(

def add_raster(
self,
source,
band=None,
palette=None,
vmin=None,
vmax=None,
nodata=None,
attribution="",
source: str,
band: Optional[List] = None,
palette: Optional[str] = None,
vmin: Optional[float] = None,
vmax: Optional[float] = None,
nodata: Optional[float] = None,
attribution: Optional[str] = "",
fit_bounds: bool = True,
layer_name="Local COG",
fit_bounds=True,
open_args={},
**kwargs,
):
Expand Down Expand Up @@ -218,22 +226,22 @@ def add_raster(

def add_stac_layer(
self,
url=None,
collection=None,
item=None,
assets=None,
bands=None,
titiler_endpoint=None,
attribution="",
fit_bounds=True,
url: str,
collection: Optional[str] = None,
item: Optional[str] = None,
assets: Optional[str] = None,
bands: Optional[List[str]] = None,
titiler_endpoint: Optional[str] = None,
attribution: Optional[str] = "",
fit_bounds: Optional[bool] = True,
open_args={},
**kwargs,
):
"""Adds a STAC TileLayer to the map.

Args:
url (str): HTTP URL to a STAC item, e.g., https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json
collection (str): The Microsoft Planetary Computer STAC collection ID, e.g., landsat-8-c2-l2.
collection (str | Optional): The Microsoft Planetary Computer STAC collection ID, e.g., landsat-8-c2-l2.
item (str): The Microsoft Planetary Computer STAC item ID, e.g., LC08_L2SP_047027_20201204_02_T1.
assets (str | list): The Microsoft Planetary Computer STAC asset ID, e.g., ["SR_B7", "SR_B5", "SR_B4"].
bands (list): A list of band names, e.g., ["SR_B7", "SR_B5", "SR_B4"]
Expand Down Expand Up @@ -304,20 +312,20 @@ def add_gdf(

def add_geojson(
self,
filename,
encoding="utf-8",
read_file_args={},
to_crs="epsg:3857",
tooltips=None,
fit_bounds=True,
filename: str,
encoding: Optional[str] = "utf-8",
read_file_args: Dict = {},
to_crs: Optional[str] = "epsg:3857",
tooltips: Optional[List] = None,
fit_bounds: bool = True,
**kwargs,
):
"""Adds a GeoJSON file to the map.

Args:
filename (str): The path to the GeoJSON file. Can be a local file or a URL.
encoding (str, optional): The encoding of the GeoJSON file. Defaults to "utf-8".
read_file_args (dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
read_file_args (Dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
to_crs (str, optional): The CRS to use for the GeoDataFrame. Defaults to "epsg:3857".
tooltips (list, optional): A list of column names to use for tooltips in the form of [(name, @column_name), ...]. Defaults to None, which uses all columns.
fit_bounds (bool, optional): A flag indicating whether to fit the map bounds to the GeoJSON. Defaults to True.
Expand All @@ -336,20 +344,20 @@ def add_geojson(

def add_shp(
self,
filename,
encoding="utf-8",
read_file_args={},
to_crs="epsg:3857",
tooltips=None,
fit_bounds=True,
filename: str,
encoding: Optional[str] = "utf-8",
read_file_args: Dict = {},
to_crs: Optional[str] = "epsg:3857",
tooltips: Optional[List] = None,
fit_bounds: bool = True,
**kwargs,
):
"""Adds a shapefile to the map.

Args:
filename (str): The path to the shapefile.
encoding (str, optional): The encoding of the shapefile. Defaults to "utf-8".
read_file_args (dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
read_file_args (Dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
to_crs (str, optional): The CRS to use for the GeoDataFrame. Defaults to "epsg:3857".
tooltips (list, optional): A list of column names to use for tooltips in the form of [(name, @column_name), ...]. Defaults to None, which uses all columns.
fit_bounds (bool, optional): A flag indicating whether to fit the map bounds to the shapefile. Defaults to True.
Expand Down Expand Up @@ -389,20 +397,20 @@ def add_shp(

def add_vector(
self,
filename,
encoding="utf-8",
read_file_args={},
to_crs="epsg:3857",
tooltips=None,
fit_bounds=True,
filename: str,
encoding: Optional[str] = "utf-8",
read_file_args: Dict = {},
to_crs: Optional[str] = "epsg:3857",
tooltips: Optional[List] = None,
fit_bounds: bool = True,
**kwargs,
):
"""Adds a vector dataset to the map.

Args:
filename (str): The path to the vector dataset. Can be a local file or a URL.
encoding (str, optional): The encoding of the vector dataset. Defaults to "utf-8".
read_file_args (dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
read_file_args (Dict, optional): A dictionary of arguments to pass to geopandas.read_file. Defaults to {}.
to_crs (str, optional): The CRS to use for the GeoDataFrame. Defaults to "epsg:3857".
tooltips (list, optional): A list of column names to use for tooltips in the form of [(name, @column_name), ...]. Defaults to None, which uses all columns.
fit_bounds (bool, optional): A flag indicating whether to fit the map bounds to the vector dataset. Defaults to True.
Expand All @@ -423,7 +431,9 @@ def add_vector(
gdf, to_crs=to_crs, tooltips=tooltips, fit_bounds=fit_bounds, **kwargs
)

def to_html(self, filename=None, title=None, **kwargs):
def to_html(
self, filename: Optional[str] = None, title: Optional[str] = None, **kwargs
):
"""Converts the map to HTML.

Args:
Expand All @@ -433,7 +443,7 @@ def to_html(self, filename=None, title=None, **kwargs):
"""
save(self.figure, filename=filename, title=title, **kwargs)

def fit_bounds(self, bounds):
def fit_bounds(self, bounds: List[float]):
"""Fits the map to the specified bounds in the form of [xmin, ymin, xmax, ymax].

Args:
Expand All @@ -447,7 +457,13 @@ def fit_bounds(self, bounds):
self.figure.y_range.start = bounds[1][0]
self.figure.y_range.end = bounds[1][1]

def to_streamlit(self, width=800, height=600, use_container_width=True, **kwargs):
def to_streamlit(
self,
width: Optional[int] = 800,
height: Optional[int] = 600,
use_container_width: bool = True,
**kwargs,
):
"""Displays the map in a Streamlit app.

Args:
Expand Down
Loading