Skip to content
Snippets Groups Projects
Commit 4ee6b59e authored by Paromita Basak's avatar Paromita Basak
Browse files

changes in .py file's Path

parent 0a979b0f
Branches main
No related tags found
No related merge requests found
import sys
import argparse
import subprocess
# Even though for this example we dont require the python gdal client, we import it to ensure gdal is installed
from osgeo import gdal
def gdal_translate(input_filename, output_filename, reduction_percent):
gdal_cmd = ["gdal_translate", "-outsize", f"{reduction_percent}%", f"{reduction_percent}%",
input_filename, output_filename]
proc = subprocess.Popen(gdal_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = proc.communicate()
print(out)
return proc.returncode, output_filename
def env_check():
# Include any code here that might need to check for the correct env setup.
print("Installed GDAL Version: " + gdal.__version__)
if __name__ == "__main__":
parse = argparse.ArgumentParser(description="Runs gdal_translate -outsize to reduce input size by n%")
parse.add_argument("--input_file", help="Input file to use", required=True)
parse.add_argument("--output_file", help="Output file to write", required=True)
parse.add_argument("--outsize", help="Reduction size", required=True)
args = parse.parse_args()
env_check()
exit_code, output = gdal_translate(args.input_file, args.output_file, args.outsize)
if exit_code != 0:
print(f"gdal_translate failed with a non-zero exit code: {exit_code}")
exit(exit_code)
import geopandas as gpd
import gdal
# Define file paths
aoi_file = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/Indonesia.shp'
input_gpkg1 = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/merged_L4AIndonesia_Subsets.gpkg'
output_gpkg1 = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Output/merged_L4AIndonesia_Subsets_Clipped.gpkg'
# Define chunk size
chunk_size = 1000
# Read AOI shapefile
aoi = gpd.read_file(aoi_file)
# Initialize a list to store clipped chunks
clipped_chunks = []
# Process input GeoPackage files in chunks
for chunk in gpd.read_file(input_gpkg1, chunksize=chunk_size):
# Reproject AOI to match CRS of current chunk
aoi_reprojected = aoi.to_crs(chunk.crs)
# Clip current chunk based on AOI
clipped_chunk = gpd.clip(chunk, aoi_reprojected)
# Append clipped chunk to the list
clipped_chunks.append(clipped_chunk)
# Concatenate clipped chunks into a single GeoDataFrame
clipped_gdf1 = gpd.GeoDataFrame(pd.concat(clipped_chunks, ignore_index=True), crs=chunk.crs)
# Save the clipped GeoPackage file
clipped_gdf1.to_file(output_gpkg1, driver='GPKG')
print("Clipping completed and saved to GeoPackage file.")
import gdal
import fiona
import geopandas
print(f"GDAL version: {gdal.__version__}")
print(f"Fiona version: {fiona.__version__}")
print(f"GeoPandas version: {geopandas.__version__}")
\ No newline at end of file
Traceback (most recent call last):
File "fiona/ogrext.pyx", line 136, in fiona.ogrext.gdal_open_vector
File "fiona/_err.pyx", line 291, in fiona._err.exc_wrap_pointer
fiona._err.CPLE_OpenFailedError: '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/merged_L4AIndonesia_Subsets.gpkg' not recognized as a supported file format.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/ClippingAlgo.py", line 21, in <module>
for chunk in gpd.read_file(input_gpkg1, chunksize=chunk_size):
File "/opt/conda/envs/vanilla/lib/python3.10/site-packages/geopandas/io/file.py", line 297, in _read_file
return _read_file_fiona(
File "/opt/conda/envs/vanilla/lib/python3.10/site-packages/geopandas/io/file.py", line 338, in _read_file_fiona
with reader(path_or_bytes, **kwargs) as features:
File "/opt/conda/envs/vanilla/lib/python3.10/site-packages/fiona/env.py", line 457, in wrapper
return f(*args, **kwds)
File "/opt/conda/envs/vanilla/lib/python3.10/site-packages/fiona/__init__.py", line 292, in open
colxn = Collection(
File "/opt/conda/envs/vanilla/lib/python3.10/site-packages/fiona/collection.py", line 243, in __init__
self.session.start(self, **kwargs)
File "fiona/ogrext.pyx", line 588, in fiona.ogrext.Session.start
File "fiona/ogrext.pyx", line 143, in fiona.ogrext.gdal_open_vector
fiona.errors.DriverError: '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/merged_L4AIndonesia_Subsets.gpkg' not recognized as a supported file format.
......@@ -2,11 +2,12 @@ import sys
import argparse
import subprocess
import geopandas as gpd
import gdal
# Define file paths
aoi_file = '/projects/Indonesia_Countrybased_90thPct/Input/Indonesia.shp'
input_gpkg1 = '/projects/Indonesia_Countrybased_90thPct/Input/merged_L4AIndonesia_Subsets.gpkg'
output_gpkg1 = '/projects/Indonesia_Countrybased_90thPct/Output/merged_L4AIndonesia_Subsets_Clipped.gpkg'
aoi_file = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/Indonesia.shp'
input_gpkg1 = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Input/merged_L4AIndonesia_Subsets.gpkg'
output_gpkg1 = '/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/Output/merged_L4AIndonesia_Subsets_Clipped.gpkg'
# Define chunk size
chunk_size = 1000
......
import gdal
import fiona
import geopandas
print(f"GDAL version: {gdal.__version__}")
print(f"Fiona version: {fiona.__version__}")
print(f"GeoPandas version: {geopandas.__version__}")
\ No newline at end of file
Traceback (most recent call last):
File "/projects/indonesiahotspots/Indonesia_Countrybased_90thPct/ClippingAlgo.py", line 5, in <module>
import gdal
ModuleNotFoundError: No module named 'gdal'
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment