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

grass.jupyter: Use InteractiveMap with custom projections #4204

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 22 additions & 17 deletions python/grass/jupyter/interactivemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import base64
import json
from pathlib import Path
from pyproj import CRS
import grass.script as gs
from .reprojection_renderer import ReprojectionRenderer, DirectRenderer

from .utils import (
Expand Down Expand Up @@ -309,36 +309,40 @@ def _import_ipyleaflet(error):
self.region = None

if crs:
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to get the CRS of the current project with g.proj, use the 'srid' key to get EPSG

if isinstance(crs, str):
crs_n = CRS.from_string(crs)
crs_dict = {"name": crs_n.to_epsg(), "custom": False}
else:
crs_dict = crs

proj = gs.read_command("g.proj", flags="jf", srid=crs)
Copy link
Contributor

Choose a reason for hiding this comment

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

I meant g.proj -g to get the srid key from the output which contains the EPSG:

g.proj -g
name=Lambert Conformal Conic
proj=lcc
datum=nad83
a=6378137.0
es=0.006694380022900787
lat_1=36.16666666666666
lat_2=34.33333333333334
lat_0=33.75
lon_0=-79
x_0=609601.22
y_0=0
no_defs=defined
srid=EPSG:3358
unit=Meter
units=Meters
meters=1

You can use parse_command

crs_dict = {"name": "crs", "proj4def": proj}
self._renderer = DirectRenderer(
crs=crs_dict, use_region=use_region, saved_region=saved_region
)
self.bounds = self._renderer.get_bbox()
self.resolution = self._renderer.get_resolution()
self.origin = self._renderer.get_origin()
custom_crs = {
"name": "custom",
"proj4def": crs_dict,
"bounds": self.bounds,
"origin": self.origin,
"resolutions": self.origin,
}

else:
self._renderer = ReprojectionRenderer(
use_region=use_region, saved_region=saved_region
)

if self._ipyleaflet:
basemap = xyzservices.providers.query_name(tiles)
if API_key and basemap.get("accessToken"):
basemap["accessToken"] = API_key
layout = self._ipywidgets.Layout(width=f"{width}px", height=f"{height}px")
if crs:
layout = self._ipywidgets.Layout(
width=f"{width}px", height=f"{height}px"
)
self.map = self._ipyleaflet.Map(
crs=crs_dict, layout=layout, scroll_wheel_zoom=True
basemap=basemap,
layout=layout,
scroll_wheel_zoom=True,
crs=custom_crs,
)
else:
basemap = xyzservices.providers.query_name(tiles)
if API_key and basemap.get("accessToken"):
basemap["accessToken"] = API_key
layout = self._ipywidgets.Layout(
width=f"{width}px", height=f"{height}px"
)
self.map = self._ipyleaflet.Map(
basemap=basemap, layout=layout, scroll_wheel_zoom=True
)
Expand All @@ -350,6 +354,7 @@ def _import_ipyleaflet(error):
tiles=tiles,
API_key=API_key, # pylint: disable=invalid-name
)

# Set LayerControl default
self.layer_control_object = None
self.region_rectangle = None
Expand Down
31 changes: 24 additions & 7 deletions python/grass/jupyter/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,47 @@ def __init__(self, use_region, saved_region, src_env):
self._use_region = use_region
self._saved_region = saved_region
self._src_env = src_env
# [SW, NE]: inverted to easily expand based on data, see _set_bbox
# Store bounding box
self._bbox = [[90, 180], [-90, -180]]
self._resolution = None
self._origin = None

if self._use_region:
self._set_bbox(self._src_env)
self._set_bbox_and_resolution(self._src_env)

if self._saved_region:
self._src_env["GRASS_REGION"] = gs.region_env(
region=self._saved_region, env=self._src_env
)
self._set_bbox(self._src_env)
self._set_bbox_and_resolution(self._src_env)

@property
def bbox(self):
return self._bbox

@property
def resolution(self):
return self._resolution

@property
def origin(self):
return self._origin

def set_region_from_raster(self, raster):
if not (self._use_region or self._saved_region):
self._src_env["GRASS_REGION"] = gs.region_env(
raster=raster, env=self._src_env
)
self._set_bbox(self._src_env)
self._set_bbox_and_resolution(self._src_env)

def set_bbox_vector(self, vector):
if not (self._saved_region or self._use_region):
env = self._src_env.copy()
env["GRASS_REGION"] = gs.region_env(vector=vector, env=env)
self._set_bbox(env)
self._set_bbox_and_resolution(env)

def _set_bbox(self, env):
bbox = gs.parse_command("g.region", flags="bg", env=env)
def _set_bbox_and_resolution(self, env):
bbox = gs.parse_command("g.region", flags="bgp", env=env)
south = float(bbox["ll_s"])
west = float(bbox["ll_w"])
north = float(bbox["ll_n"])
Expand All @@ -166,6 +176,13 @@ def _set_bbox(self, env):
self._bbox[1][0] = max(self._bbox[1][0], north)
self._bbox[1][1] = max(self._bbox[1][1], east)

nsres = float(bbox["nsres"])
ewres = float(bbox["ewres"])
self._resolution = [ewres, nsres]

# Set origin (west, north)
self._origin = [west, north]


class RegionManagerFor2D:
"""Region manager for 2D displays (gets region from display commands)"""
Expand Down
6 changes: 6 additions & 0 deletions python/grass/jupyter/reprojection_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ def __init__(self, crs, use_region=False, saved_region=None, work_dir=None):
def get_bbox(self):
return self._region_manager.bbox

def get_resolution(self):
return self._region_manager.resolution

def get_origin(self):
return self._region_manager.origin

def render_raster(self, name):
file_info = gs.find_file(name, element="cell", env=self._src_env)
full_name = file_info["fullname"]
Expand Down