1

I use python and GDAL under apache to make a webservice.

I have a GeoTIFF file and i want only a subset of the data, everything inside one polygon defined by a buffer of a line (coord is the input parameter of the service):

line = ogr.Geometry(ogr.wkbLineString)
for i in range(len(coord)):
    line.AddPoint(coord[i][0], coord[i][1])

line.Transform(coordTransform)    
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon = line.Buffer(10.0, 2)

I find only solutions with shapefile, not with programmatically created geometries. Any help?

2
  • solved with: points = [] pixels = [] pts = polygon.GetGeometryRef(0) for p in range(pts.GetPointCount()): points.append((pts.GetX(p), pts.GetY(p))) for p in points: pixels.append(world2Pixel(geoTrans, p[0], p[1]))
    – Luca Guida
    Commented Mar 25, 2016 at 9:54
  • if that solved your questions please post it as an answer and accept it (little tick on the left). This way others with the same problem can easier find the solution in the future.
    – Kersten
    Commented Mar 28, 2016 at 9:20

1 Answer 1

1

solved with:

[...]    
srcArray = gdalnumeric.LoadFile(rasterFile)

pxWidth = int(lrX - ulX)
pxHeight = int(lrY - ulY)

points = []
pixels = []
pts = poligono.GetGeometryRef(0)
for p in range(pts.GetPointCount()):
    points.append((pts.GetX(p), pts.GetY(p)))
for p in points:
    pixels.append(conf.world2Pixel(geoTrans, p[0], p[1]))

for i in range(len(pixels)):
    pixels[i] = (pixels[i][0]-ulX, pixels[i][1]-ulY)

rasterPoly = Image.new("L", (pxWidth, pxHeight), 0)
rasterize = ImageDraw.Draw(rasterPoly)
rasterize.polygon(pixels, 1)
mask = conf.imageToArray(rasterPoly)

valTiff = []
for r in range(pxHeight):
    for c in range(pxWidth):
        if mask[r][c]==1:
            valTiff.append(srcArray[r+ulY][c+ulX])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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