Skip to content
Draft
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
166 changes: 166 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Custom
.vscode
data
project_docs
qgis
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.7
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# plantilla
Plantilla de proyecto
# Dencity


49 changes: 49 additions & 0 deletions notebooks/heuristic_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# %%
import rasterio
from rasterio.plot import show

from preprocessing.utils import calculate_ndvi, mask_raster_band_by_value

MDS_INPUT_FILE = "data/ideuy/K29D6P6_MDS_Remesa_01_MVD.tif"
MDT_INPUT_FILE = "data/ideuy/K29D6P6_MDT_Remesa_01_MVD.tif"
RGBI_INPUT_FILE = "data/ideuy/K29D6P6_RGBI_16_Remesa_01_MVD.tif"
NDVI_OUTPUT_FILE = "data/ideuy/K29D6P6_NDVI_16_Remesa_01_MVD.tif"
DIFF_OUTPUT_FILE = "data/ideuy/K29D6P6_MVD_diff.tif"

# %%
# Read data
mds_raster = rasterio.open(MDS_INPUT_FILE)
mdt_raster = rasterio.open(MDT_INPUT_FILE)
rgbi_raster = rasterio.open(RGBI_INPUT_FILE)

# %%
# Plot rasters
show(mds_raster)
show(mdt_raster)
show(rgbi_raster)

# %%
# Raster operations: MDS - MDT

# Mask no-data values (value == -32767.0)
mds_no_data_value = mds_raster.read(1).min()
mdt_no_data_value = mdt_raster.read(1).min()

mds_masked_raster = mask_raster_band_by_value(
raster_band=mds_raster.read(1), no_data_value=mds_no_data_value
)
mdt_masked_raster = mask_raster_band_by_value(
raster_band=mdt_raster.read(1), no_data_value=mdt_no_data_value
)

# diff_raster = mds_masked_raster - mdt_masked_raster

# %%

# Calculate NDVI
ndvi_raster = calculate_ndvi(input_rgbi_raster_file=RGBI_INPUT_FILE, output_file=NDVI_OUTPUT_FILE)

# Plot raster
show(ndvi_raster)

# %%
22 changes: 22 additions & 0 deletions preprocessing/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# System
NUM_PARALLEL_JOBS = -2

# Geographic
CRS_EPSG = 5382

# Raster
RASTER_SCALE_FACTOR = 1 / 4

# Data output
OUTPUT_VECTORS = "data/outputs/vector"
OUTPUT_RASTERS = "data/outputs/raster"
MDS_OUTPUT_FILE = f"{OUTPUT_RASTERS}/K29D6P6_MDS_MVD_025x.tif"
NDVI_OUTPUT_FILE = f"{OUTPUT_RASTERS}/K29D6P6_NDVI_16_Remesa_01_MVD.tif"

# Data input
INPUT_VECTORS = ""
INPUT_RASTERS_IDEUY = "data/ideuy"

MDS_INPUT_FILE = f"{INPUT_RASTERS_IDEUY}/K29D6P6_MDS_Remesa_01_MVD.tif"
MDT_INPUT_FILE = f"{INPUT_RASTERS_IDEUY}/K29D6P6_MDT_Remesa_01_MVD.tif"
RGBI_INPUT_FILE = f"{INPUT_RASTERS_IDEUY}/K29D6P6_RGBI_16_Remesa_01_MVD.tif"
29 changes: 29 additions & 0 deletions preprocessing/raster_to_polygons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# %%
from pathlib import Path

import rasterio
from constants import MDT_INPUT_FILE, OUTPUT_VECTORS
from rasterio import features
from utils import mask_raster_band_by_value, raster_to_polygons

# %%
if __name__ == "__main__":
raster = rasterio.open(MDT_INPUT_FILE)
# Set mask based on no-data values
raster_no_data_value = raster.read(1).min()
mdt_masked_raster = mask_raster_band_by_value(
raster_band=raster.read(1), no_data_value=raster_no_data_value
)
# Get polygon and value from each raster cell:
masked_raster_polygons = features.shapes(mdt_masked_raster)
# Convert to polygons
masked_polygons_gdf = raster_to_polygons(
shapes_from_raster=masked_raster_polygons, parallel=True
)
# Write geodataframe
filename = Path(MDT_INPUT_FILE).name
output_file = Path(OUTPUT_VECTORS) / f"{filename}.geojson"
print(f"Saving output to {output_file}")
Path(OUTPUT_VECTORS).mkdir(parents=True, exist_ok=True)
masked_polygons_gdf.to_file(output_file, driver="GeoJSON")
print("Done!")
15 changes: 15 additions & 0 deletions preprocessing/rgb_downsampling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pathlib import Path

from constants import (
MDS_INPUT_FILE,
MDS_OUTPUT_FILE,
OUTPUT_RASTERS,
RASTER_SCALE_FACTOR,
)
from utils import save_raster, transform_raster

if __name__ == "__main__":
Path(OUTPUT_RASTERS).mkdir(parents=True, exist_ok=True)
original_rgb, trans_rgb, transform = transform_raster(MDS_INPUT_FILE, RASTER_SCALE_FACTOR)
save_raster(original_rgb, trans_rgb, transform, MDS_OUTPUT_FILE)
print("Done!")
Loading