0

I'm completely new to python usage in GIS. I'm working with Sentinel images and want to sample the pixels of raster file only in the places which are represented by the geometry of the truth data which I have it in the form of shapefile. All I want to find is the analogous of the below code in Python.

var training = image.select(bands).sampleRegions({
  collection: points,
  properties: [class],
  scale: 30
});

image.select(bands) - is my raster data,

Collection- is my shapefile

Trying to find my output as the below image. Source : https://miro.medium.com/max/1050/1*WrtLqr49lbpf24K3Pb6ViQ.png [![enter image description here][1]][1]

https://www.earthdatascience.org/courses/use-data-open-source-python/intro-raster-data-python/raster-data-processing/classify-plot-raster-data-in-python/

I wanted to do some sampling like in the above link, it has clustering and sampling of trees based on their heights but I want to sample and cluster pixels only in the locations based on the geometries of my shapefile i.e, wanted to acheive supervised classification.

2
  • Hi, if I understand correctly, you want to extract pixel values at certain locations. These locations are given in a shapefile. Then, this should do the job: rasterio.readthedocs.io/en/latest/topics/…
    – Jan Pisl
    Aug 5, 2020 at 12:00
  • Hi Yes that's almost right. I want to sample pixels in the given locations of shapefile and give them a class like in the above code. Aug 6, 2020 at 4:51

1 Answer 1

0

You can use this to find the pixel:

def Pixel2world( geoMatrix, x, y ):
    xOrigin   = geoMatrix[0]
    yOrigin   = geoMatrix[3]
    pixelWidth  = geoMatrix[1]
    pixelHeight = geoMatrix[5]
    coorX = ( xOrigin + ( x * pixelWidth ))
    coorY = ( yOrigin + ( y * pixelHeight ))
    return coorX, coorY
2
  • I have the raster file in the form of tif file. Aug 6, 2020 at 7:17
  • 1
    Could you please format the code in your answer so that it is readable multiple lines, rather than one "run-on"? This answer on Meta SE explains how to format code in answers. Aug 6, 2020 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.