Closed brumasribera closed 6 years ago
Could you post what your input data and command is that produces the error message?
The pixel_value_from_point
method is well tested and should work. Have a look at how to use it in the test examples https://github.com/geodesign/django-raster/blob/ea6f5aa94dd8d9ff8fab9e13d675905c787d78c6/tests/test_utils.py#L92
You are right, instead of using a GDALRaster I'm using a RasterLayer. Would there still be a way of using this method with a RasterLayer?
There is an url endpoint to get pixel values from raster algebra expressions.
https://django-raster.readthedocs.io/en/0.6/algebra.html#pixel-value-lookup
You could get the layer pixel value by calling this endpoint with a "trivial" formula like so:
/raster/pixel/-9218229/3229269/?layers=x=23&formula=x
where -9218229/3229269
is your location in web mercator projection (srid 3857), and 23
would be your raster layer id.
If you want to do it in a script or custom view, you need to determine first which RasterTile covers your point. This is a bit clunky, but something along the lines of the example below should work.
from raster.models import RasterLayer, RasterTile
from raster.utils import pixel_value_from_point
from raster.tiles.utils import tile_index_range
from raster.tiles.const import WEB_MERCATOR_SRID
from raster.tiles.lookup import get_raster_tile
# Get raster layer and its maximum parsed zoom level.
layer = RasterLayer.objects.get(pk=23)
zlevel = layer.max_zoom
# Compute point extent in web mercator coordinates.
point = OGRGeometry('SRID=4326;POINT(0 0)')
point.transform(WEB_MERCATOR_SRID)
bbox = point.extent
# Determine which tile index intersects with this point.
indexrange = tile_index_range(bbox, zlevel)
tilex = indexrange[0]
tiley = indexrange[1]
# Get raster tile
tile = get_raster_tile(layer.id, zlevel, tilex, tiley)
# Extract pixel value from raster.
result = pixel_value_from_point(tile.rast, point.coords)
Oh! I just saw that you closed the issue and realized I didn't answer. Thank you so much for your helpful response, at the end we didn't incorporate the feature that required this twist, but if we come back in the future to it will definitely come in handy :) Thank you for your great work Daniel!
You are most welcome @howna13 , I am glad to know that you found the package useful! Let me know if your mapping project is publicly accessible. I am curious to see what django-raster is being used for.
Hey @yellowcap, so we are extracting information from satellite images and creating an API to make this data available for outdoor apps to use it. If you want to know more you can check our website wegaw.com and more specifically the project site defrost.ch, thank you again for your efforts!
Thanks a lot for the transparency @howna13 ! Looks like a great project, all the best for your venture. Don't hesitate to raise more issues if you find bugs or have questions / suggestions.
Hello,
I'm getting the following error when calling the pixel_value_from_point method:
I'm afraid the error comes from the line 153 where instead of
bbox = OGRGeometry.from_bbox(raster.extent)
You should do:
bbox = OGRGeometry.from_bbox(raster.extent())
Hope this make sense.