Skip to content

Commit

Permalink
Improved rasterisation of shapefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
sambowers committed Dec 17, 2018
1 parent 9acc90b commit 2a13e3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion biota/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def extractGamma0(dataloc, year, shp, plot_field, agb_field, buffer_size = 0, ve

# For each tile covered by shapefile
for lat, lon in tiles:

if verbose: print 'Doing lat: %s, lon: %s'%(str(lat), str(lon))

# Load tile
Expand Down
12 changes: 6 additions & 6 deletions biota/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def _world2Pixel(geo_t, x, y, buffer_size = 0):
xDist = geo_t[1]
yDist = geo_t[5]

pixel = int((x - ulX) / xDist)
line = int((y - ulY) / yDist)
pixel = (x - ulX) / xDist
line = (y - ulY) / yDist

return (pixel, line)

Expand Down Expand Up @@ -332,27 +332,27 @@ def maskShapefile(tile, shp, buffer_size = 0., field = None, value = None, locat

# Then convert map to pixel coordinates using geo transform
pixels.append(_world2Pixel(tile.geo_t, lon, lat, buffer_size = buffer_size_degrees))

# Draw the mask for this shape...
# if a point...
if shape.shapeType == 0 or shape.shapeType == 1 or shape.shapeType == 11:
rasterize.point(pixels, n+1)

# a line...
elif shape.shapeType == 3 or shape.shapeType == 13:
rasterize.line(pixels, n+1)

# or a polygon.
elif shape.shapeType == 5 or shape.shapeType == 15:
rasterize.polygon(pixels, n+1)

else:
print 'Shapefile type %s not recognised!'%(str(shape.shapeType))

#Converts a Python Imaging Library array to a gdalnumeric image.
mask = gdalnumeric.fromstring(rasterPoly.tobytes(),dtype=np.uint32)
mask.shape = rasterPoly.im.size[1], rasterPoly.im.size[0]

# If any buffer pixels are slected, dilate the masked area by buffer_px pixels
if buffer_px > 0:
mask = dilateMask(mask, buffer_px, location_id = location_id)
Expand Down

0 comments on commit 2a13e3f

Please sign in to comment.