Closed CARocha closed 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.
Here is an example on how to "reproject" lat/lon coordinates. https://stackoverflow.com/questions/17664327/leaflet-panto-web-mercator-coordinates-epsg-3857
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
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!!
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.
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).
Hi, @yellowcap same error i change the url include layers and formula, convert values int but same error, look the screenshot
i put the urls in browser i see this
my id rasterlayer is 4 see admin screenshot
because that url doesn't return anything to me, just not found, I do not understand anything :(
any idea :/
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
Closing due to inactivity - hope you figured this out.
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:
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?
thanks, if you can help me with any idea, if that is possible or some link on how to do it