GDAL/OGR to extract pixel values The Next CEO of Stack OverflowRunning a Python script to...

Mathematica command that allows it to read my intentions

How can the PCs determine if an item is a phylactery?

Is it okay to majorly distort historical facts while writing a fiction story?

Finitely generated matrix groups whose eigenvalues are all algebraic

Car headlights in a world without electricity

Noise during hard braking

Does int main() need a declaration on C++?

Words hidden in my phone number

Direct Implications Between USA and UK in Event of No-Deal Brexit

How do I keep Mac Emacs from trapping M-`?

Percent Dissociated from Titration Curve

Read/write a pipe-delimited file line by line with some simple text manipulation

Why do we say “un seul M” and not “une seule M” even though M is a “consonne”?

Incomplete cube

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

Horror film about a man brought out of cryogenic suspension without a soul, around 1990

Plausibility of squid whales

Trying to insert a background image via TikZ results in extra white-space around the corners

It it possible to avoid kiwi.com's automatic online check-in and instead do it manually by yourself?

Creating a script with console commands

How to find if SQL server backup is encrypted with TDE without restoring the backup

Why was Sir Cadogan fired?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Calculate the Mean mean of two numbers



GDAL/OGR to extract pixel values



The Next CEO of Stack OverflowRunning a Python script to extract raster data in QGISGetting pixel value of GDAL raster under OGR point without NumPy?Determining if shapefile and raster overlap in Python using OGR/GDAL?Extract raster values gdal looping rastersCreate new shapefile based on values ranges in existing shapefile using gdal/ogr/pythonProblem intersecting shapefiles with OGR in PythonGDAL/OGR ogr.Layer.Intersection fails when producing mixed geometry resultsCreate ShapeFile from CSV Using OGR and PythonRasterizing Shapefile with attribute value as pixel value with GDAL in PythonExtract raster values within shapefile with pygeoprocessing or gdalCut shapefile to raster in Python using GDAL/OGR












5















I want to use GDAL/OGR to extract pixel values from a raster file. The extraction should be based on polygons, stored in a shapefile. My thought was to iterate over the features in the shapefile, extract the pixel values of those pixels that have there center point within the polygon and calculate a percentile from those values. When this is done I want to write the calculated percentile back to the polygon and go on with the next polygon.



Iterating over the polygons is easy, but what I do not get is how to extract the pixel values from the pixels, covered by the polygon.



I'm using Python.



Could somebody help me at this point?










share|improve this question

























  • If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

    – Thomas Becker
    Aug 13 '13 at 11:37






  • 1





    It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

    – MappaGnosis
    Aug 13 '13 at 11:56













  • @MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

    – whuber
    Aug 13 '13 at 15:13






  • 1





    alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

    – Thomas Becker
    Aug 13 '13 at 16:04








  • 1





    @WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

    – MappaGnosis
    Aug 14 '13 at 15:07
















5















I want to use GDAL/OGR to extract pixel values from a raster file. The extraction should be based on polygons, stored in a shapefile. My thought was to iterate over the features in the shapefile, extract the pixel values of those pixels that have there center point within the polygon and calculate a percentile from those values. When this is done I want to write the calculated percentile back to the polygon and go on with the next polygon.



Iterating over the polygons is easy, but what I do not get is how to extract the pixel values from the pixels, covered by the polygon.



I'm using Python.



Could somebody help me at this point?










share|improve this question

























  • If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

    – Thomas Becker
    Aug 13 '13 at 11:37






  • 1





    It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

    – MappaGnosis
    Aug 13 '13 at 11:56













  • @MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

    – whuber
    Aug 13 '13 at 15:13






  • 1





    alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

    – Thomas Becker
    Aug 13 '13 at 16:04








  • 1





    @WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

    – MappaGnosis
    Aug 14 '13 at 15:07














5












5








5








I want to use GDAL/OGR to extract pixel values from a raster file. The extraction should be based on polygons, stored in a shapefile. My thought was to iterate over the features in the shapefile, extract the pixel values of those pixels that have there center point within the polygon and calculate a percentile from those values. When this is done I want to write the calculated percentile back to the polygon and go on with the next polygon.



Iterating over the polygons is easy, but what I do not get is how to extract the pixel values from the pixels, covered by the polygon.



I'm using Python.



Could somebody help me at this point?










share|improve this question
















I want to use GDAL/OGR to extract pixel values from a raster file. The extraction should be based on polygons, stored in a shapefile. My thought was to iterate over the features in the shapefile, extract the pixel values of those pixels that have there center point within the polygon and calculate a percentile from those values. When this is done I want to write the calculated percentile back to the polygon and go on with the next polygon.



Iterating over the polygons is easy, but what I do not get is how to extract the pixel values from the pixels, covered by the polygon.



I'm using Python.



Could somebody help me at this point?







python gdal ogr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 6 '16 at 2:44









PolyGeo

53.8k1781245




53.8k1781245










asked Aug 13 '13 at 10:57









Thomas BeckerThomas Becker

577719




577719













  • If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

    – Thomas Becker
    Aug 13 '13 at 11:37






  • 1





    It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

    – MappaGnosis
    Aug 13 '13 at 11:56













  • @MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

    – whuber
    Aug 13 '13 at 15:13






  • 1





    alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

    – Thomas Becker
    Aug 13 '13 at 16:04








  • 1





    @WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

    – MappaGnosis
    Aug 14 '13 at 15:07



















  • If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

    – Thomas Becker
    Aug 13 '13 at 11:37






  • 1





    It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

    – MappaGnosis
    Aug 13 '13 at 11:56













  • @MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

    – whuber
    Aug 13 '13 at 15:13






  • 1





    alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

    – Thomas Becker
    Aug 13 '13 at 16:04








  • 1





    @WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

    – MappaGnosis
    Aug 14 '13 at 15:07

















If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

– Thomas Becker
Aug 13 '13 at 11:37





If I iterate over the features and while one is 'selected', can I call the GetExtent function of the shapefile layer and will the returned extent only represent the extent of the current feature?

– Thomas Becker
Aug 13 '13 at 11:37




1




1





It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

– MappaGnosis
Aug 13 '13 at 11:56







It sounds as though you are wanting to do zonal statistics. Have a look at gis.stackexchange.com/questions/43748/…"

– MappaGnosis
Aug 13 '13 at 11:56















@MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

– whuber
Aug 13 '13 at 15:13





@MappaGnosis That version of zonal stats appears extremely limited; I see no evidence from the documentation that it computes percentiles. Do you know of a zonal stats solution for QGIS that does compute specified percentiles by zone?

– whuber
Aug 13 '13 at 15:13




1




1





alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

– Thomas Becker
Aug 13 '13 at 16:04







alright, the percentile is no problem for me since I had to define a function for it anyway. My issue goes with the Python coding using GDAL/OGR... I'm used to do the stuff using ArcGIS and their Python classes. However, it takes ages and in the end the script is crashing. So, I'm on my way to re-write the script without arcpy. Can anybody point me to some code snippet where I can learn on how to do the stuff?

– Thomas Becker
Aug 13 '13 at 16:04






1




1





@WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

– MappaGnosis
Aug 14 '13 at 15:07





@WHuber Offhand I don't know of a more extensive Zonal Stats Plugin for QGIS. However, my link was meant to point to the programmed Python + GDAL solution in my reply in that thread. My example there is just looking at getting the minimum value, but being a programmed solution, it shouldn't be too hard to extend it a bit to calculate percentiles of the zone data. Instead of going for the 'easy statistics' options there is also a scipy.ndimage.filters.percentile_filter function available.

– MappaGnosis
Aug 14 '13 at 15:07










1 Answer
1






active

oldest

votes


















2














Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.



Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3



In that blog post I rasterize vector data to subset pixels from an image.



The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.



You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.



Next, you can do the rest of the calculations.






share|improve this answer





















  • 2





    +1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

    – Aaron
    Mar 15 '16 at 23:15













  • That link doesn't work anymore sadly

    – Cam
    Mar 28 at 12:45











  • @cam link fixed to an archive version

    – Akhorus
    6 mins ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f68562%2fgdal-ogr-to-extract-pixel-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.



Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3



In that blog post I rasterize vector data to subset pixels from an image.



The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.



You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.



Next, you can do the rest of the calculations.






share|improve this answer





















  • 2





    +1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

    – Aaron
    Mar 15 '16 at 23:15













  • That link doesn't work anymore sadly

    – Cam
    Mar 28 at 12:45











  • @cam link fixed to an archive version

    – Akhorus
    6 mins ago
















2














Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.



Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3



In that blog post I rasterize vector data to subset pixels from an image.



The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.



You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.



Next, you can do the rest of the calculations.






share|improve this answer





















  • 2





    +1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

    – Aaron
    Mar 15 '16 at 23:15













  • That link doesn't work anymore sadly

    – Cam
    Mar 28 at 12:45











  • @cam link fixed to an archive version

    – Akhorus
    6 mins ago














2












2








2







Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.



Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3



In that blog post I rasterize vector data to subset pixels from an image.



The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.



You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.



Next, you can do the rest of the calculations.






share|improve this answer















Your question defines a complex process which is difficult to help with all of it. Here, I will suggest something that might help with the part getting the raster data using the vector.



Refer to this: http://machinalis8.rssing.com/chan-51920729/latest.php#item3



In that blog post I rasterize vector data to subset pixels from an image.



The idea there is that you convert the vector data into a raster, like a mask. Then, using numpy, you use that mask to filter or choose the pixels in the raster.



You may need to check the part about "the center of the pixel" fallin within the poligon: it will depend on GDAL's implementation of RasterizeLayer.



Next, you can do the rest of the calculations.







share|improve this answer














share|improve this answer



share|improve this answer








edited 7 mins ago

























answered Mar 15 '16 at 21:14









AkhorusAkhorus

301211




301211








  • 2





    +1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

    – Aaron
    Mar 15 '16 at 23:15













  • That link doesn't work anymore sadly

    – Cam
    Mar 28 at 12:45











  • @cam link fixed to an archive version

    – Akhorus
    6 mins ago














  • 2





    +1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

    – Aaron
    Mar 15 '16 at 23:15













  • That link doesn't work anymore sadly

    – Cam
    Mar 28 at 12:45











  • @cam link fixed to an archive version

    – Akhorus
    6 mins ago








2




2





+1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

– Aaron
Mar 15 '16 at 23:15







+1 Nice use of random forests in your example. It would be helpful to include an example on this site in the event that the link goes away.

– Aaron
Mar 15 '16 at 23:15















That link doesn't work anymore sadly

– Cam
Mar 28 at 12:45





That link doesn't work anymore sadly

– Cam
Mar 28 at 12:45













@cam link fixed to an archive version

– Akhorus
6 mins ago





@cam link fixed to an archive version

– Akhorus
6 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f68562%2fgdal-ogr-to-extract-pixel-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown