Skip to content

Commit

Permalink
Update more modules
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Nov 16, 2024
1 parent 35a5fe4 commit bf008a2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
30 changes: 15 additions & 15 deletions leafmap/bokehmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bokeh.plotting import figure, show, save
from bokeh.io import output_notebook
from .basemaps import xyz_to_bokeh
from .common import *
from . import common
from typing import Optional, List, Dict

os.environ["OUTPUT_NOTEBOOK"] = "False"
Expand Down Expand Up @@ -36,9 +36,9 @@ def __init__(
kwargs["height"] = height

if "x_range" not in kwargs:
kwargs["x_range"] = center_zoom_to_xy_range(center, zoom)[0]
kwargs["x_range"] = common.center_zoom_to_xy_range(center, zoom)[0]
if "y_range" not in kwargs:
kwargs["y_range"] = center_zoom_to_xy_range(center, zoom)[1]
kwargs["y_range"] = common.center_zoom_to_xy_range(center, zoom)[1]

fig = figure(**kwargs)
self.figure = fig
Expand Down Expand Up @@ -150,7 +150,7 @@ def add_cog_layer(
apply a rescaling to multiple bands, use something like `rescale=["164,223","130,211","99,212"]`.
**kwargs: Arbitrary keyword arguments for bokeh.figure.add_tile() function, such as alpha, visible, etc.
"""
tile_url = cog_tile(url, bands, titiler_endpoint, **cog_args)
tile_url = common.cog_tile(url, bands, titiler_endpoint, **cog_args)
tile_options = {
"url": tile_url,
"attribution": attribution,
Expand All @@ -159,7 +159,7 @@ def add_cog_layer(
self.figure.add_tile(tile_source, **kwargs)

if fit_bounds:
self.fit_bounds(cog_bounds(url, titiler_endpoint))
self.fit_bounds(common.cog_bounds(url, titiler_endpoint))

def add_raster(
self,
Expand Down Expand Up @@ -197,9 +197,9 @@ def add_raster(
"""

if source.startswith("http"):
source = download_file(source)
source = common.download_file(source)

tile_layer, client = get_local_tile_layer(
tile_layer, client = common.get_local_tile_layer(
source,
indexes=indexes,
colormap=colormap,
Expand Down Expand Up @@ -252,7 +252,7 @@ def add_stac_layer(
**kwargs: Arbitrary keyword arguments for bokeh.figure.add_tile() function, such as alpha, visible, etc.
"""
tile_url = stac_tile(
tile_url = common.stac_tile(
url, collection, item, assets, bands, titiler_endpoint, **open_args
)
tile_options = {
Expand All @@ -263,7 +263,7 @@ def add_stac_layer(
self.figure.add_tile(tile_source, **kwargs)

if fit_bounds:
self.fit_bounds(stac_bounds(url, collection, item, titiler_endpoint))
self.fit_bounds(common.stac_bounds(url, collection, item, titiler_endpoint))

def add_gdf(
self,
Expand All @@ -287,7 +287,7 @@ def add_gdf(
if not isinstance(gdf, gpd.GeoDataFrame):
raise TypeError("gdf must be a GeoDataFrame")

geom_type = gdf_geom_type(gdf)
geom_type = common.gdf_geom_type(gdf)
gdf_new = gdf.to_crs(to_crs)

columns = gdf_new.columns.to_list()
Expand Down Expand Up @@ -340,7 +340,7 @@ def add_geojson(
import geopandas as gpd

if filename.startswith("http"):
filename = github_raw_url(filename)
filename = common.github_raw_url(filename)

gdf = gpd.read_file(filename, encoding=encoding, **read_file_args)
self.add_gdf(
Expand Down Expand Up @@ -374,15 +374,15 @@ def add_shp(
import glob

if filename.startswith("http"):
filename = github_raw_url(filename)
filename = common.github_raw_url(filename)

if filename.startswith("http") and filename.endswith(".zip"):
out_dir = os.path.abspath("./cache/shp")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
basename = os.path.basename(filename)
output = os.path.join(out_dir, basename)
download_file(filename, output)
common.download_file(filename, output)
files = list(glob.glob(os.path.join(out_dir, "*.shp")))
if len(files) > 0:
filename = files[0]
Expand Down Expand Up @@ -425,7 +425,7 @@ def add_vector(
import geopandas as gpd

if filename.startswith("http"):
filename = github_raw_url(filename)
filename = common.(filename)

if isinstance(filename, gpd.GeoDataFrame):
gdf = filename
Expand Down Expand Up @@ -455,7 +455,7 @@ def fit_bounds(self, bounds: List[float]):
bounds (list): A list of bounds in the form of [xmin, ymin, xmax, ymax].
"""

bounds = bounds_to_xy_range(bounds)
bounds = common.bounds_to_xy_range(bounds)

self.figure.x_range.start = bounds[0][0]
self.figure.x_range.end = bounds[0][1]
Expand Down
10 changes: 6 additions & 4 deletions leafmap/deck.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from .common import *
from .osm import *
from .leafmap import basemaps
from . import common
from . import examples
from . import osm
from .leafmap import basemaps

from typing import Optional

try:
import pydeck as pdk
Expand Down Expand Up @@ -144,7 +146,7 @@ def add_gdf(
raise TypeError("gdf must be a GeoPandas GeoDataFrame.")

if layer_name is None:
layer_name = "layer_" + random_string(3)
layer_name = "layer_" + common.random_string(3)

if "layer_type" == "GeoJsonLayer":
if "pickable" not in kwargs:
Expand Down
16 changes: 8 additions & 8 deletions leafmap/deckgl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import Union, List, Dict, Optional, Tuple, Any
from .basemaps import xyz_to_leaflet
from .common import *
from .map_widgets import *
from .plot import *
from . import common
from . import map_widgets
from . import plot

try:
import lonboard
Expand Down Expand Up @@ -165,7 +165,7 @@ def add_gdf(
if src_crs is None:
src_crs = "EPSG:4326"

lon, lat = convert_coordinates(x, y, src_crs, "EPSG:4326")
lon, lat = common.convert_coordinates(x, y, src_crs, "EPSG:4326")

self.view_state = {
"latitude": lat,
Expand Down Expand Up @@ -296,7 +296,7 @@ def to_html(self, filename: Optional[str] = None) -> None:
"""

if filename is None:
filename = temp_file_path("html")
filename = common.temp_file_path("html")
super().to_html(filename)
with open(filename) as f:
html = f.read()
Expand Down Expand Up @@ -353,7 +353,7 @@ def add_basemap(self, basemap="HYBRID", visible=True, **kwargs) -> None:

if isinstance(basemap, str):
if basemap.upper() in map_dict:
tile = get_google_map(basemap.upper())
tile = common.get_google_map(basemap.upper())

layer = lonboard.BitmapTileLayer(
data=tile.url,
Expand Down Expand Up @@ -489,9 +489,9 @@ def add_raster(
import xarray as xr

if isinstance(source, np.ndarray) or isinstance(source, xr.DataArray):
source = array_to_image(source, **array_args)
source = common.array_to_image(source, **array_args)

tile_layer, tile_client = get_local_tile_layer(
tile_layer, tile_client = common.get_local_tile_layer(
source,
indexes=indexes,
colormap=colormap,
Expand Down
1 change: 1 addition & 0 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import examples
from . import map_widgets
from . import plot
from .common import set_api_key, get_api_key, planet_quarterly_tiles

from branca.element import Figure, JavascriptLink, MacroElement
from folium.elements import JSCSSMixin
Expand Down
35 changes: 21 additions & 14 deletions leafmap/kepler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import pandas as pd
from IPython.display import display, HTML
from .common import *
from .osm import *
from . import common
from . import osm
from . import examples
from typing import Optional, Union, Dict
from pandas import DataFrame
Expand Down Expand Up @@ -129,6 +129,7 @@ def add_geojson(
FileNotFoundError: The provided GeoJSON file could not be found.
TypeError: The input geojson must be a type of str or dict.
"""
import shutil

if "encoding" in kwargs:
encoding = kwargs["encoding"]
Expand All @@ -138,7 +139,7 @@ def add_geojson(
try:
if isinstance(in_geojson, str):
if in_geojson.startswith("http"):
if is_jupyterlite():
if common.is_jupyterlite():
import pyodide

output = os.path.basename(in_geojson)
Expand All @@ -150,7 +151,7 @@ def add_geojson(
with open(output, "r") as fd:
data = json.load(fd)
else:
in_geojson = github_raw_url(in_geojson)
in_geojson = common.github_raw_url(in_geojson)
data = requests.get(in_geojson).json()
else:
in_geojson = os.path.abspath(in_geojson)
Expand Down Expand Up @@ -195,7 +196,7 @@ def add_shp(
out_dir = os.path.abspath("./cache/shp")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
download_from_url(in_shp, out_dir=out_dir, verbose=False)
common.download_from_url(in_shp, out_dir=out_dir, verbose=False)
files = list(glob.glob(os.path.join(out_dir, "*.shp")))
if len(files) > 0:
in_shp = files[0]
Expand All @@ -208,7 +209,7 @@ def add_shp(
if not os.path.exists(in_shp):
raise FileNotFoundError("The provided shapefile could not be found.")

geojson = shp_to_geojson(in_shp)
geojson = common.shp_to_geojson(in_shp)
self.add_geojson(
geojson,
layer_name,
Expand All @@ -232,7 +233,7 @@ def add_gdf(
"""

data = gdf_to_geojson(gdf, epsg="4326")
data = common.gdf_to_geojson(gdf, epsg="4326")
self.add_geojson(data, layer_name, **kwargs)
self.load_config(config)

Expand Down Expand Up @@ -310,7 +311,7 @@ def add_vector(
)
self.load_config(config)
else:
geojson = vector_to_geojson(
geojson = common.vector_to_geojson(
filename,
epsg="4326",
**kwargs,
Expand Down Expand Up @@ -345,7 +346,9 @@ def add_kml(
out_dir = os.path.abspath("./cache")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
download_from_url(in_kml, out_dir=out_dir, unzip=False, verbose=False)
common.download_from_url(
in_kml, out_dir=out_dir, unzip=False, verbose=False
)
in_kml = os.path.join(out_dir, os.path.basename(in_kml))
if not os.path.exists(in_kml):
raise FileNotFoundError("The downloaded kml file could not be found.")
Expand Down Expand Up @@ -378,7 +381,7 @@ def add_gdf_from_postgis(
config (str, optional): Local path or HTTP URL to the config file. Defaults to None.
"""
gdf = read_postgis(sql, con, **kwargs)
gdf = common.read_postgis(sql, con, **kwargs)
gdf = gdf.to_crs("epsg:4326")
self.add_gdf(
gdf,
Expand Down Expand Up @@ -410,17 +413,21 @@ def static_map(
if isinstance(self, keplergl.KeplerGl):
if out_file is None:
if os.environ.get("USE_MKDOCS") is not None:
out_file = "../maps/" + "kepler_" + random_string(3) + ".html"
out_file = (
"../maps/" + "kepler_" + common.random_string(3) + ".html"
)
else:
out_file = "./cache/" + "kepler_" + random_string(3) + ".html"
out_file = (
"./cache/" + "kepler_" + common.random_string(3) + ".html"
)
out_dir = os.path.abspath(os.path.dirname(out_file))
if not os.path.exists(out_dir):
os.makedirs(out_dir)

output = widgets.Output()
with output:
self.save_to_html(file_name=out_file, read_only=read_only)
display_html(src=out_file, width=width, height=height)
common.display_html(src=out_file, width=width, height=height)
else:
raise TypeError("The provided map is not a kepler.gl map.")

Expand Down Expand Up @@ -451,7 +458,7 @@ def to_html(
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
outfile = os.path.abspath(random_string() + ".html")
outfile = os.path.abspath(common.random_string() + ".html")
save = False

output = widgets.Output()
Expand Down
7 changes: 3 additions & 4 deletions leafmap/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,10 +2174,9 @@ def oam_search(
GeoDataFrame | list: If return_gdf is True, return a GeoDataFrame. Otherwise, return a list.
"""

if return_gdf:
import pandas as pd
from shapely.geometry import Polygon
import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon
import geopandas as gpd

url = "https://api.openaerialmap.org/meta"
if bbox is not None:
Expand Down

0 comments on commit bf008a2

Please sign in to comment.