Comments (6)
@brendan-ward Yes, rasterizing would be a good feature.
I think we could simplify it quite a bit by having the same constraint as gdal_rasterize, that the vectors and raster have the same CRS, no reprojection. Also, I think GeoJSON-ish Python objects (the kind of things you could pass to shapely.geometry.shape()
) are a better fit for rasterio than Shapely objects.
What would you think of adding a rasterio.features.rasterize() function that is symmetric with features.shapes()? Usage something like:
from rasterio.features import rasterize, shapes
# get the shapes and values from "source" and rasterize them into "destination".
destination = rasterize(shapes(source, transform=None), source.shape, transform=None)
the above should produce a destination array equal to the source. A round trip. Rasterio is all about symmetry :)
Of course, the shapes above would be in image coordinates. Passing not-None transforms in would let us use coordinates appropriate to a CRS.
from rasterio.
@brendan-ward Most importantly: pull request would be very welcome! Feel free to grab code from rasterio/_features.pyx.
from rasterio.
Sorry, all. I'm going to keep these issues on track by deleting tangential comments.
from rasterio.
Do we want to broaden this beyond simple binary masks, and allow parameters on this function to allow creation of either a mask, or rasterize values of a given property on the features?
I was also planning to add this in _features.pyx, since this seems highly related, rather than creating a new file. Sound good?
from rasterio.
Progress! I do think there has to be a better way to do some of these things - like looping over the features.
def _rasterize(features, size, transform=None):
"""
:param features: fiona style feature iterator
:param size: (rows, cols)
:param transform: GDAL style transform. If provided, will be set on output
"""
cdef int retval, rows, cols
cdef size_t i, num_features
cdef void *memdriver
cdef void *out_ds
cdef void *out_band
cdef double geotransform[6]
cdef double pixel_values[1]
cdef int dst_bands[1]
pixel_values[0] = 1 #TODO: Allow other pixel values
rows, cols = size
#Do the boilerplate required to create a band
memdriver = _gdal.GDALGetDriverByName("MEM")
if memdriver == NULL:
raise ValueError("NULL driver for 'MEM'")
out_ds = _gdal.GDALCreate(memdriver, "output", cols, rows, 1, <_gdal.GDALDataType>1, NULL) #TODO: revisit data type
if out_ds == NULL:
raise ValueError("NULL output datasource")
if transform:
for i in range(6):
geotransform[i] = transform[i]
err = _gdal.GDALSetGeoTransform(out_ds, geotransform)
if err:
raise ValueError("transform not set: %s" % transform)
out_band = _gdal.GDALGetRasterBand(out_ds, 1)
if out_band == NULL:
raise ValueError("NULL output band")
dst_bands[0] = 1
#TODO: figure out a cleaner way to do this
features_json = []
for feature in features: #have to loop over features, since it may be yielded from a generator
features_json.append(json.dumps(feature['geometry']))
num_features = len(features_json)
cdef void **ogr_geoms = <void **>_gdal.CPLMalloc(num_features * sizeof(void*))
try:
for i in range(num_features):
ogr_geoms[i] = _ogr.OGR_G_CreateGeometryFromJson(features_json[i])
#TODO: add options
retval = _gdal.GDALRasterizeGeometries(out_ds, 1, dst_bands, 1, ogr_geoms, NULL, geotransform, pixel_values, NULL, NULL, NULL)
out = np.zeros(size, np.uint8) #FIXME - data type
retval = io_ubyte(out_band, 0, 0, 0, cols, rows, out) #FIXME - data type
finally:
_gdal.CPLFree(ogr_geoms)
if out_ds != NULL:
_gdal.GDALClose(out_ds)
return out
from rasterio.
Exciting!
from rasterio.
Related Issues (20)
- Build rasterio main and maint on new GDAL tags HOT 5
- `rio` cli for `rio merge` is missing the `--method` attribute HOT 2
- CRSError: The WKT could not be parsed.
- Fails to build with GDAL 3.8.0 HOT 1
- 1.2.9+ EPSG and OGC coordinate systems comp HOT 6
- Support for GDAL 3.8 (via conda-forge)
- WindowError: Bounds and transform are inconsistent HOT 7
- Warn about potential for deadlock with multiprocessing and fork()
- Support for GDALs TPS transformer for GCP georeferencing HOT 1
- values of bands are not the same in QGIS HOT 4
- Incorrect no data filling on the edges during merge operation
- Per-thread vsiopener file registries and exit stacks
- boundless=True - hangs in docker container rio read HOT 1
- DEP: Use numpy in pyproject.toml HOT 5
- Reproject XSCALE & YSCALE defaults
- Rasterio merge gives different results with different python versions HOT 2
- Overhaul test_plot.py
- Simplify rasterize() HOT 1
- Readthedocs stable build out of date HOT 2
- plot.show for (n,1) and (1,n) numpy arrays HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rasterio.