Extract points in a shape from a raster

I have a raster file (basically a 2D array) with an approximate number of points. I am trying to extract a circle from a raster (and all the points that lie inside the circle). Using ArcGIS for this is extremely slow. Can anyone suggest any image processing library that will be easy to learn, and powerful enough and fast enough for something like that?

Thank!

+3
source share
3 answers

Retrieving a subset of the points effectively depends on the exact format you are using. Assuming you store your raster array as an integer array of integers, you can extract the following points:

from numpy import *

def points_in_circle(circle, arr):
    "A generator to return all points whose indices are within given circle."
    i0,j0,r = circle
    def intceil(x):
        return int(ceil(x))
    for i in xrange(intceil(i0-r),intceil(i0+r)):
        ri = sqrt(r**2-(i-i0)**2)
        for j in xrange(intceil(j0-ri),intceil(j0+ri)):
            yield arr[i][j]

points_in_circle , . , yield return. , , . . . Python , yield.

, . , , . , , .

points_in_circle:

# raster dimensions, 10 million points
N, M = 3200, 3200
# circle center and its radius in index space
i0, j0, r = 70, 20, 12.3

raster = fromfunction(lambda i,j: 100+10*i+j, (N, M), dtype=int)
print "raster is ready"
print raster

pts_iterator = points_in_circle((i0,j0,r), raster) # very quick, do not extract points yet
pts = array(list(pts_iterator)) # actually extract all points
print pts.size, "points extracted, sum = ", sum(pts)

10 .

, -, .

+2

Numpy :

import numpy

all_points = numpy.random.random((1000, 1000))  # Input array
# Size of your array of points all_points:
(image_size_x, image_size_y) = all_points.shape
# Disk definition:
(center_x, center_y) = (500, 500)
radius = 10

x_grid, y_grid = numpy.meshgrid(numpy.arange(image_size_x),
                                numpy.arange(image_size_y))
# Array of booleans with the disk shape
disk = ((x_grid-center_x)**2 + (y_grid-center_y)**2) <= radius**2

# You can now do all sorts of things with the mask "disk":

# For instance, the following array has only 317 points (about pi*radius**2):
points_in_disk = all_points[disk]
# You can also use masked arrays:
points_in_circle2 = numpy.ma.masked_array(all_points, ~disk)
from matplotlib import pyplot
pyplot.imshow(points_in_circle2)
+2

, . , Python, geotools ( ), Java. C, - GDAL.

, , QGIS python, .

, PostGIS . , , , SQL .

, .

+1
source

Source: https://habr.com/ru/post/1744081/


All Articles