Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldorman committed Oct 8, 2023
1 parent c0135f7 commit 41783d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions 05-raster-vector.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -916,24 +916,24 @@ Alternatively, we can use the `osgeo` interface to GDAL to do the same, using th
Theis makes the code much longer (then just one command, as above), but avoids explicitly running a separate program (`gdal_contour`) outside of the Python environment (since we focus on `rasterio`, explaining the code is beyond the scope of this book):

```{python}
input_r = osgeo.gdal.Open('data/dem.tif', osgeo.gdalconst.GA_ReadOnly)
input_r1 = input_r.GetRasterBand(1)
sr = osgeo.osr.SpatialReference(input_r.GetProjection())
ogr_ds = osgeo.ogr.GetDriverByName('GPKG').CreateDataSource('output/dem_contour2.gpkg')
contour_shp = ogr_ds.CreateLayer('contours', sr)
field_defn = osgeo.ogr.FieldDefn('ID', osgeo.ogr.OFTInteger)
contour_shp.CreateField(field_defn)
field_defn = osgeo.ogr.FieldDefn('elev', osgeo.ogr.OFTReal)
contour_shp.CreateField(field_defn)
osgeo.gdal.ContourGenerate(input_r1, 50, 0, [], 0, 0, contour_shp, 0, 1)
ogr_ds = None
# input_r = osgeo.gdal.Open('data/dem.tif', osgeo.gdalconst.GA_ReadOnly)
# input_r1 = input_r.GetRasterBand(1)
# sr = osgeo.osr.SpatialReference(input_r.GetProjection())
# ogr_ds = osgeo.ogr.GetDriverByName('GPKG').CreateDataSource('output/dem_contour2.gpkg')
# contour_shp = ogr_ds.CreateLayer('contours', sr)
# field_defn = osgeo.ogr.FieldDefn('ID', osgeo.ogr.OFTInteger)
# contour_shp.CreateField(field_defn)
# field_defn = osgeo.ogr.FieldDefn('elev', osgeo.ogr.OFTReal)
# contour_shp.CreateField(field_defn)
# osgeo.gdal.ContourGenerate(input_r1, 50, 0, [], 0, 0, contour_shp, 0, 1)
# ogr_ds = None
```

Here is the identical result of the second approach, named `dem_contour2.gpkg`:

```{python}
contours2 = gpd.read_file('output/dem_contour2.gpkg')
contours2
# contours2 = gpd.read_file('output/dem_contour2.gpkg')
# contours2
```

Here is a plot of the identical contour layers `dem_contour1.gpkg` and `dem_contour2.gpkg` created using the two methods (@fig-raster-contours2):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ topojson==1.5
xarray==2022.12.0
Cartopy==0.22.0
osmnx==1.6.0
contextily==1.3.0
contextily==1.3.0

0 comments on commit 41783d9

Please sign in to comment.