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

how do i get the map color with leaflet #37

Closed CARocha closed 4 years ago

CARocha commented 5 years ago

Hi @yellowcap, sorry for the inconvenience, but I need to know if there is any way to capture the color of the map when I click on any point, or at least some information from the legends where I put the colors? this is my map with all color:

Captura de pantalla 2019-09-08 a la(s) 11 13 43

you su when click i capture the lat and lng, but the color yellow no :/ the question is: How can I do to capture the color when I click on the map?

or capture information from the legends entry either the name, the code or the expression?

Captura de pantalla 2019-09-08 a la(s) 11 17 14

thanks, if you can help me with any idea, if that is possible or some link on how to do it

yellowcap commented 4 years ago

I don't think Leaflet lets you extract pixel values from TMS layers. However, with a bit of Javascript you can call another url endpoint from the django-raster package. There is an endpoint to request a single pixel value from coordinates in the Web Mercator projection (EPSG 3857).

https://django-raster.readthedocs.io/en/stable/algebra.html?highlight=pixel#pixel-value-lookup

So you can add a get request triggered by the "MouseClick" js event. In your case you would have to reproject the lat/lon coordinates into web mercator to make the request.

yellowcap commented 4 years ago

Here is an example on how to "reproject" lat/lon coordinates. https://stackoverflow.com/questions/17664327/leaflet-panto-web-mercator-coordinates-epsg-3857

CARocha commented 4 years ago

Hi, @yellowcap thank por the link but i can't find the solution, use the proj4 js file http://proj4js.org/

this is my code js: map.on('click',function(e){ lat = e.latlng.lat; lon = e.latlng.lng; //--------------------------- var source = new proj4.Proj('EPSG:3857'); var dest = new proj4.Proj('EPSG:4326');

var p = proj4.transform(dest,source, [lon,lat]);

 var color_pixel = L.tileLayer('http://localhost:8000/raster/pixel/'+p.x+'/'+p.y+'/', {
    attribution: 'Something'
}).addTo(map);

console.log(p);
console.log(color_pixel);

//Add a marker to show where you clicked.
theMarker = L.marker([lat,lon],{draggable:true}).addTo(map);

});

and this is error

Captura de pantalla 2019-09-11 a la(s) 22 46 58

I don't understand why the url throws an error

What am I doing wrong, I just want the color of that pixel?

thank for your help!!

yellowcap commented 4 years ago

You need to include the layers and formula parameter. The pixel endpiont requires a "raster algebra" expression. For a simple lookup of the raster without any transformation use http://localhost:8000/raster/pixel/x_coord/y_coord/?layers=a=LAYERID&formula=a where the LAYERID is the ID of the rasterlayer you are currently displaying.

yellowcap commented 4 years ago

I.e. http://localhost:8000/raster/pixel/-941530/1450984/?layers=a=23432&formula=a where the rasterlayer id would be 23432.

Also, maybe you have to omit the decimal values from the coordinates in the url (not sure if it works with decimals as well).

CARocha commented 4 years ago

Hi, @yellowcap same error i change the url include layers and formula, convert values int but same error, look the screenshot

Captura de pantalla 2019-09-12 a la(s) 19 10 36

i put the urls in browser i see this

Captura de pantalla 2019-09-12 a la(s) 19 11 11

my id rasterlayer is 4 see admin screenshot

Captura de pantalla 2019-09-12 a la(s) 19 17 02

because that url doesn't return anything to me, just not found, I do not understand anything :(

any idea :/

yellowcap commented 4 years ago

can you try without the trainling slash? I see that the url patterns from django-raster might not accept the last slash, so try this:

http://localhost:8000/raster/pixel/-941530/1450984?layers=a=4&formula=a

yellowcap commented 4 years ago

Closing due to inactivity - hope you figured this out.