I am running the following code and get:
Segmentation fault (core dumped)
I have no clue why, I have been using the functions many times before and now suddenly this happens. Sometimes it happens at the first read of ReadAsArray, and somtimes after 20+ reads.
#!/usr/bin/python3
import numpy as np
from methods import sampler
maps = [
'{}_slope.tif'.format(continent) for continent in ['au', 'eu']
]
def create_tiles(size):
for ulLat in range(90, -91+size, -size):
for ulLon in range(-180, 181-size, size):
yield ulLon, ulLat, ulLon + size, ulLat - size
def sample_point_xy(px, py, rb, cols, rows, win_size=1, func=None):
if (px >= 0) & (px <= cols) & (py >= 0) & (py <= rows): # check if within map extent
if win_size > 1:
intval = rb.ReadAsArray(
int(px - win_size / 2.), int(py - win_size / 2.), win_size, win_size)
else:
intval = rb.ReadAsArray(
int(px), int(py), win_size, win_size)
if func is not None:
value = func(intval)
else:
value = intval
else:
value = np.nan
return value
def get_samples():
size = 4
for m in maps:
rb, gt, cols, rows = sampler.get_raster_info(m)
for tile in create_tiles(size):
ulLon, ulLat, lrLon, lrLat = tile
ulx, uly = sampler.MapToPixel(ulLon, ulLat, gt)
lrx, lry = sampler.MapToPixel(lrLon, lrLat, gt)
win_size = int(max(lrx-ulx, uly-lry))
px, py = (ulx + lrx) / 2, (uly + lry) / 2
sample = sample_point_xy(px, py, rb, cols, rows, win_size=win_size)
yield sample
for sample in get_samples():
if sample is not None and not np.isnan(sample):
print(sample)
print(sum(sum(sample)))