use_crs¶The use_crs parameter is supported by all geometry layers which have the map parameter, i.e. by layers which can be used for vizualization of Geopandas GeoDataFrame.
Those layers are: point, path, polygon, text, label and map.
By default, Lets-Plot converts all coordinates in every GeoDataFrame to degrees of longitude and latitude and leter projects these map coordinates to screen coordinates useing Mercator projection.
Sometimes it might be necessary to use some other coordinate reference system (CRS) instead of the default WGS84 / Mercator.
If this is the case, you can use the use_crs parameter to specify EPSG code of CRS to project GeoDataFrame coordinates to.
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
Let's say we have a raster image (for example, a satellite or aerial photograph, or a DEM image) that covers a square lend area with:
In practice, the image coordinates and the image coordinate reference system (CRS) are known from a metadata accompanying the image.
Here, for the sake of simplicity, we will use just a simple generated image 2 x 2 pix.
We will also presume that our image coordinates use UTM zone 33N CRS (aka EPSG:32633).
Thus, the image coordinates are given in meters instead of more familiar longitude and latatude.
# image 2 x 2 pix
image_arr = np.array([
[[150, 0, 0], [0, 150, 0]],
[[0, 0, 150], [150, 150, 0]]
])
image_CRS = "EPSG:32633"
# Image coordinates
min_x = 25_000
max_x = 525_000
min_y = 4_500_000
max_y = 5_000_000
ggplot() + geom_imshow(image_arr, extent=[min_x, max_x, min_y, max_y])
Step 1 here would be to aquare a Geopandas GeoDataFrame containing map of the area of interesst.
In this demo we will use GeoDataFrame containing a map of Italy which we will obtain using Lets-Plot geocoding module.
from lets_plot.geo_data import *
italy = geocode_countries('italy').inc_res().get_boundaries()
ggplot() + geom_map(map=italy)
italy.crs
The image uses UTM CRS with coordinates in meters and the "Italy map" uses the EPSG:4326 CRS with coordinates in degrees of longitude and latitude.
Due to this discrepancy we can not right away combine these two layes in one plot.
use_crs parameter¶The solution here is actually pretty straightforward: we will just let the map layer know which exactly CRS the other plot layer uses (i.e. which CRS our image uses).
ggplot() + geom_map(map=italy, use_crs=image_CRS) + labs(x="", y="")
# Image and map
( ggplot()
+ geom_imshow(image_arr, extent=[min_x, max_x, min_y, max_y])
+ geom_map(map=italy, use_crs=image_CRS)
+ labs(x="", y="")
)
# Select better theme and colors
( ggplot()
+ geom_imshow(image_arr, extent=[min_x, max_x, min_y, max_y])
+ geom_map(map=italy, use_crs=image_CRS, color="#a6b6ba", size=.8)
+ labs(x="", y="")
+ theme_bw()+ flavor_solarized_dark()
+ ggsize(500, 500)
)