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

Improve add_vector methods #921

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
122 changes: 48 additions & 74 deletions leafmap/foliumap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,35 +1357,14 @@ def add_shp(
Raises:
FileNotFoundError: The provided shapefile could not be found.
"""
import glob
import geopandas as gpd

if in_shp.startswith("http") and in_shp.endswith(".zip"):
out_dir = os.path.abspath("./cache/shp")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
basename = os.path.basename(in_shp)
filename = os.path.join(out_dir, basename)
# download_from_url(in_shp, out_dir=out_dir, verbose=False)
download_file(in_shp, filename)
files = list(glob.glob(os.path.join(out_dir, "*.shp")))
if len(files) > 0:
in_shp = files[0]
else:
raise FileNotFoundError(
"The downloaded zip file does not contain any shapefile in the root directory."
)
else:
in_shp = os.path.abspath(in_shp)
if not os.path.exists(in_shp):
raise FileNotFoundError("The provided shapefile could not be found.")

data = shp_to_geojson(in_shp)

self.add_geojson(
data,
layer_name=layer_name,
info_mode=info_mode,
zoom_to_layer=zoom_to_layer,
gdf = gpd.read_file(in_shp)
self.add_gdf(
gdf,
layer_name,
zoom_to_layer,
info_mode,
**kwargs,
)

Expand All @@ -1412,8 +1391,10 @@ def add_geojson(
FileNotFoundError: The provided GeoJSON file could not be found.
"""
import json
import requests
import random
import geopandas as gpd

gdf = None

try:
if isinstance(in_geojson, str):
Expand All @@ -1430,24 +1411,29 @@ def add_geojson(
with open(output, "r") as fd:
data = json.load(fd)
else:
in_geojson = github_raw_url(in_geojson)
data = requests.get(in_geojson).json()
gdf = gpd.read_file(in_geojson, encoding=encoding)

else:
in_geojson = os.path.abspath(in_geojson)
if not os.path.exists(in_geojson):
raise FileNotFoundError(
"The provided GeoJSON file could not be found."
)

with open(in_geojson, encoding=encoding) as f:
data = json.load(f)
gdf = gpd.read_file(in_geojson, encoding=encoding)

elif isinstance(in_geojson, dict):
data = in_geojson
gdf = gpd.GeoDataFrame.from_features(in_geojson)
elif isinstance(in_geojson, gpd.GeoDataFrame):
gdf = in_geojson
else:
raise TypeError("The input geojson must be a type of str or dict.")
except Exception as e:
raise Exception(e)

if gdf.crs is None:
print(
f"Warning: The dataset does not have a CRS defined. Assuming EPSG:4326."
)
gdf.crs = "EPSG:4326"
elif gdf.crs != "EPSG:4326":
gdf = gdf.to_crs("EPSG:4326")
data = gdf.__geo_interface__

# interchangeable parameters between ipyleaflet and folium.

if "style_callback" in kwargs:
Expand Down Expand Up @@ -1515,7 +1501,7 @@ def random_color(feature):
geojson.add_to(self)

if zoom_to_layer:
bounds = get_bounds(data)
bounds = bounds = gdf.total_bounds
self.zoom_to_bounds(bounds)

def add_gdf(
Expand All @@ -1540,26 +1526,14 @@ def add_gdf(
if gdf[col].dtype in ["datetime64[ns]", "datetime64[ns, UTC]"]:
gdf[col] = gdf[col].astype(str)

data = gdf_to_geojson(gdf, epsg="4326")

self.add_geojson(
data,
gdf,
layer_name=layer_name,
info_mode=info_mode,
zoom_to_layer=zoom_to_layer,
**kwargs,
)

# if zoom_to_layer:
# import numpy as np

# bounds = gdf.to_crs(epsg="4326").bounds
# west = np.min(bounds["minx"])
# south = np.min(bounds["miny"])
# east = np.max(bounds["maxx"])
# north = np.max(bounds["maxy"])
# self.fit_bounds([[south, east], [north, west]])

def add_gdf_from_postgis(
self,
sql: str,
Expand Down Expand Up @@ -1626,9 +1600,7 @@ def add_kml(
if not os.path.exists(in_kml):
raise FileNotFoundError("The provided KML could not be found.")

data = kml_to_geojson(in_kml)

self.add_geojson(data, layer_name=layer_name, info_mode=info_mode, **kwargs)
self.add_vector(in_kml, layer_name, info_mode=info_mode, **kwargs)

def add_vector(
self,
Expand All @@ -1653,31 +1625,33 @@ def add_vector(
zoom_to_layer (bool, optional): Whether to zoom to the layer. Defaults to True.

"""
if not filename.startswith("http"):
filename = os.path.abspath(filename)
import fiona
import geopandas as gpd

ext = os.path.splitext(filename)[1].lower()
if ext == ".shp":
self.add_shp(filename, layer_name, **kwargs)
elif ext in [".json", ".geojson"]:
self.add_geojson(filename, layer_name, **kwargs)
if isinstance(filename, str) and filename.endswith(".kml"):
fiona.drvsupport.supported_drivers["KML"] = "rw"
gdf = gpd.read_file(
filename,
bbox=bbox,
mask=mask,
rows=rows,
driver="KML",
)
else:
geojson = vector_to_geojson(
gdf = gpd.read_file(
filename,
bbox=bbox,
mask=mask,
rows=rows,
epsg="4326",
**kwargs,
)

self.add_geojson(
geojson,
layer_name,
info_mode=info_mode,
zoom_to_layer=zoom_to_layer,
**kwargs,
)
self.add_gdf(
gdf,
layer_name,
zoom_to_layer,
info_mode,
**kwargs,
)

def add_planet_by_month(
self,
Expand Down
Loading