# ConvertingDEM **Repository Path**: cai_ya_jun/ConvertingDEM ## Basic Information - **Project Name**: ConvertingDEM - **Description**: Brief application of geopandas, gdal and other packages to create a shapefile with contour curves from coarse DEM - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-12 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README This notebook aims to show how easly we could convert a raster file into a polygon, extracting or reclassifying the raster to certain values before the conversion ##### Import module ```python import gdal ``` ##### Hint: To open a raster with gdal- just use that ```python gdal_data = gdal.Open(r"PAN_alt.tif") gdal_band = gdal_data.GetRasterBand(1) ``` ##### Importing path ```python path = r"PAN_alt.tif" out_file = path.replace(".tif", "_recl.tif") ``` ##### Cheking the polygonize options ```python !gdal_polygonize.py ``` gdal_polygonize [-8] [-nomask] [-mask filename] raster_file [-b band|mask] [-q] [-f ogr_format] out_file [layer] [fieldname] ##### Checking the calculator utility ```python !gdal_calc.py ``` Usage: gdal_calc.py --calc=expression --outfile=out_filename [-A filename] [--A_band=n] [-B...-Z filename] [other_options] Options: -h, --help show this help message and exit --calc=expression calculation in gdalnumeric syntax using +-/* or any numpy array functions (i.e. log10()) -A filename input gdal raster file, you can use any letter (A-Z) --A_band=n number of raster band for file A (default 1) --outfile=filename output file to generate or fill --NoDataValue=value output nodata value (default datatype specific value) --type=datatype output datatype, must be one of ['Byte', 'UInt16', 'Int16', 'UInt32', 'Int32', 'Float32', 'Float64'] --format=gdal_format GDAL format for output file --creation-option=option, --co=option Passes a creation option to the output format driver. Multiple options may be listed. See format specific documentation for legal creation options for each format. --allBands=[A-Z] process all bands of given raster (A-Z) --overwrite overwrite output file if it already exists --debug print debugging information --quiet suppress progress messages #### Our try will be to use a basic raster calculation routine to calculate a new raster where the following values will replace some ranges in the digital elevation model 1 = 0 - 1000 2 = 1001 - 2000 3 = 2001 - 3000 4 = 3001 - 4000 ```python !gdal_calc.py -A "PAN_alt.tif" --outfile "PAN_alt_recl.tif" --calc="(logical_and(0 ![png](05_Converting_Contour_Curves_to_Polygons_files/05_Converting_Contour_Curves_to_Polygons_17_2.png) ```python shp ```
DN geometry
0 2 POLYGON ((-83.2 9.666666668, -83.1916666670000...
1 1 POLYGON ((-79.56666681199999 9.641666668999999...
2 0 POLYGON ((-82.55000002600001 9.566666671999998...
3 0 POLYGON ((-82.51666669400001 9.533333339999999...
4 2 POLYGON ((-83.19166666700001 9.575000005, -83....
5 0 POLYGON ((-82.49166669500001 9.516666674, -82....
6 0 POLYGON ((-82.525000027 9.491666674999999, -82...
7 0 POLYGON ((-82.500000028 9.491666674999999, -82...
8 0 POLYGON ((-82.425000031 9.458333343, -82.41666...
9 2 POLYGON ((-82.941666677 9.441666676999999, -82...
10 0 POLYGON ((-82.266666704 9.441666676999999, -82...
11 0 POLYGON ((-82.941666677 9.433333343999999, -82...
12 2 POLYGON ((-82.791666683 9.416666677999999, -82...
13 0 POLYGON ((-82.908333345 9.400000012, -82.90000...
14 0 POLYGON ((-79.825000135 9.400000012, -79.81666...
15 1 POLYGON ((-82.291666703 9.441666676999999, -82...
16 2 POLYGON ((-83.2 9.400000012, -83.1916666670000...
17 0 POLYGON ((-83.19166666700001 9.383333345999999...
18 0 POLYGON ((-83.000000008 9.375000012999999, -82...
19 0 POLYGON ((-82.95833334300001 9.358333346999999...
20 1 POLYGON ((-82.233333372 9.358333346999999, -82...
21 1 POLYGON ((-82.95833334300001 9.350000013999999...
22 0 POLYGON ((-82.250000038 9.350000013999999, -82...
23 0 POLYGON ((-82.125000043 9.325000014999999, -82...
24 0 POLYGON ((-82.208333373 9.316666681999999, -82...
25 0 POLYGON ((-82.16666670800001 9.316666681999999...
26 1 POLYGON ((-82.891666679 9.308333349, -82.88333...
27 0 POLYGON ((-82.20000004000001 9.300000015999998...
28 1 POLYGON ((-82.175000041 9.291666682999999, -82...
29 0 POLYGON ((-82.183333374 9.266666683999999, -82...
... ... ...
216 2 POLYGON ((-78.216666866 7.708333412999999, -78...
217 1 POLYGON ((-81.633333396 7.700000079999999, -81...
218 2 POLYGON ((-78.158333535 7.750000077999999, -78...
219 2 POLYGON ((-78.041666873 7.675000080999999, -78...
220 1 POLYGON ((-81.35000007400001 7.658333415, -81....
221 1 POLYGON ((-81.708333393 7.650000081999999, -81...
222 1 POLYGON ((-80.000000128 7.633333415999999, -79...
223 2 POLYGON ((-77.708333553 7.975000068999999, -77...
224 2 POLYGON ((-78.01666687399999 7.583333417999999...
225 1 POLYGON ((-81.216666746 7.575000084999999, -81...
226 2 POLYGON ((-78.000000208 7.550000085999999, -77...
227 2 POLYGON ((-77.52500022700001 7.600000083999999...
228 2 POLYGON ((-77.983333542 7.633333415999999, -77...
229 1 POLYGON ((-81.108333417 7.583333417999999, -81...
230 2 POLYGON ((-80.716666766 7.500000087999999, -80...
231 2 POLYGON ((-77.916666878 7.500000087999999, -77...
232 1 POLYGON ((-82.241666705 7.483333421999999, -82...
233 2 POLYGON ((-77.925000211 7.475000089, -77.91666...
234 2 POLYGON ((-80.633333436 7.358333427, -80.62500...
235 2 POLYGON ((-80.658333435 7.333333428, -80.65000...
236 1 POLYGON ((-81.750000058 7.650000081999999, -81...
237 2 POLYGON ((-80.758333431 7.341666760999999, -80...
238 2 POLYGON ((-80.691666767 7.366666759999999, -80...
239 2 POLYGON ((-80.758333431 7.308333428999999, -80...
240 2 POLYGON ((-80.78333343 7.291666762999999, -80....
241 2 POLYGON ((-80.775000097 7.283333429999999, -80...
242 1 POLYGON ((-81.825000055 7.291666762999999, -81...
243 1 POLYGON ((-81.808333389 7.225000098999999, -81...
244 1 POLYGON ((-81.800000056 7.216666765999999, -81...
245 1 POLYGON ((-83.2 9.699999999999999, -82.8000000...

246 rows × 2 columns