geodesign / django-raster

Django-raster allows you to create tiled map services (TMS) and raster map algebra end points for web maps. It is Python-based, and requires GeoDjango with a PostGIS backend.
BSD 3-Clause "New" or "Revised" License
96 stars 39 forks source link

TypeError: 'method' object is not iterable in pixel_value_from_point #33

Closed brumasribera closed 6 years ago

brumasribera commented 6 years ago

Hello,

I'm getting the following error when calling the pixel_value_from_point method:

File "/usr/local/lib/python3.6/site-packages/raster/utils.py", line 153, in pixel_value_from_point
    bbox = OGRGeometry.from_bbox(raster.extent)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/gis/gdal/geometries.py", line 158, in from_bbox
    x0, y0, x1, y1 = bbox
TypeError: 'method' object is not iterable

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.

yellowcap commented 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

brumasribera commented 6 years ago

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?

yellowcap commented 6 years ago

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)
brumasribera commented 6 years ago

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!

yellowcap commented 6 years ago

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.

brumasribera commented 5 years ago

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!

yellowcap commented 5 years ago

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.