Skip to content
Extraits de code Groupes Projets
Valider cf575709 rédigé par Boris Nörgaard's avatar Boris Nörgaard
Parcourir les fichiers

test2

parent bc8e7c91
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
%% Cell type:markdown id: tags:
# Region of Interest
%% Cell type:code id: tags:
``` python
import os
import geopandas as gpd
from shapely.geometry import Polygon
from pathlib import Path
import matplotlib.pyplot as plt
from IPython.display import display
print('All libraries successfully imported!')
print(f'GeoPandas : {gpd.__version__}')
print('TEST')
print('TEST2')
```
%% Output
All libraries successfully imported!
GeoPandas : 0.9.0
%% Cell type:markdown id: tags:
## Set directory
%% Cell type:code id: tags:
``` python
computer_path = '/export/miro/ndeffense/LBRAT2104/'
#computer_path = 'H:/ndeffense/LBRAT2104/'
#computer_path = 'X:/'
grp_letter = 'X'
# Directory for all work files
work_path = f'{computer_path}GROUP_{grp_letter}/WORK/'
# Directory where ROI shapefile is stored
roi_path = f'{work_path}ROI/'
# Create ROI path if not exists
Path(roi_path).mkdir(parents=True, exist_ok=True)
```
%% Cell type:markdown id: tags:
## Choose CRS of your ROI
Choose the same CRS as the Sentinel data.
E.g. : WGS 84 / UTM zone 31N (EPSG:32631) if your ROI is located in Belgium
%% Cell type:code id: tags:
``` python
crs_dst = 'EPSG:32631'
```
%% Cell type:markdown id: tags:
## Give a name for the ROI shapefile
Tip : You can include the EPSG code in the filename!
%% Cell type:code id: tags:
``` python
roi_name = 'extent_roi'
roi_filename = f'{roi_name}_{crs_dst[5:]}.shp'
roi_file = f'{roi_path}{roi_filename}'
print(f'ROI shapefile will be stored in : {roi_file}')
```
%% Output
ROI shapefile will be stored in : H:/ndeffense/LBRAT2104/GROUP_X/WORK/ROI/extent_roi_32631.shp
%% Cell type:markdown id: tags:
## Find the coordinates of your ROI bounding box (bbox)
To find the coordinates of a bounding box, check: http://bboxfinder.com/
**!! Be carefull of the crs_src you chose !!**
%% Cell type:code id: tags:
``` python
crs_src = 'EPSG:32631'
bbox = [627263.7,5590485.2,637115,5596175.1]
ulx = bbox[0]
uly = bbox[1]
lrx = bbox[2]
lry = bbox[3]
print(f'Upper Left X : {ulx}')
print(f'Upper Left Y : {uly}')
print(f'Lower Right X : {lrx}')
print(f'Lower Right Y : {lry}')
```
%% Output
Upper Left X : 627263.7
Upper Left Y : 5590485.2
Lower Right X : 637115
Lower Right Y : 5596175.1
%% Cell type:markdown id: tags:
## Store your ROI into a GeoDataFrame
%% Cell type:code id: tags:
``` python
# Create a list with the longitude coordinates (x)
lon_point_list = [ulx, lrx, lrx, ulx, ulx]
# Create a list with the latitude coordinates (y)
lat_point_list = [uly, uly, lry, lry, uly]
# Create a polygon object from the two list (lon and lat)
polygon_geom = Polygon(zip(lon_point_list, lat_point_list))
# Create a GeoDataFrame with the polygon object
gdf = gpd.GeoDataFrame(index=[0], crs=crs_src, geometry=[polygon_geom])
# Reproject the GeoDataFrame to the destination CRS (needed only if you set your coordinates in WGS 84)
gdf = gdf.to_crs(crs_dst)
display(gdf)
# Check CRS of your polygon
print(f'ROI CRS : {gdf.crs}')
```
%% Output
ROI CRS : EPSG:32631
%% Cell type:markdown id: tags:
### Add a name
%% Cell type:code id: tags:
``` python
gdf['name'] = 'ROI_wallonia'
display(gdf)
```
%% Output
%% Cell type:markdown id: tags:
## Plot your ROI
Is it a nice rectangular polygon?
%% Cell type:code id: tags:
``` python
fig, ax = plt.subplots(1, 1)
gdf.boundary.plot(ax=ax)
plt.box(False)
```
%% Output
%% Cell type:markdown id: tags:
## Write GeoDataFrame in a shapefile if not exists
%% Cell type:code id: tags:
``` python
if not os.path.isfile(roi_file):
gdf.to_file(filename=roi_file, driver='ESRI Shapefile')
print(f'A new vector file is created : {roi_file}')
else:
print('The ROI vector file already exists --> delete it or change the variable "roi_name" if you want to create a new one')
```
%% Output
The ROI vector file already exists --> delete it or change the variable "roi_name" if you want to create a new one
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter