Finding Raster Pixel Neighbors using GDAL? Planned maintenance scheduled April 23, 2019 at...
Why is it faster to reheat something than it is to cook it?
What is the meaning of 'breadth' in breadth first search?
Deconstruction is ambiguous
How to report t statistic from R
Drawing spherical mirrors
Co-worker has annoying ringtone
Random body shuffle every night—can we still function?
Most bit efficient text communication method?
What does Turing mean by this statement?
Did any compiler fully use 80-bit floating point?
What's the point of the test set?
What order were files/directories output in dir?
How many morphisms from 1 to 1+1 can there be?
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
Why are my pictures showing a dark band on one edge?
What does this say in Elvish?
How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?
Google .dev domain strangely redirects to https
What initially awakened the Balrog?
An adverb for when you're not exaggerating
Semigroups with no morphisms between them
macOS: Name for app shortcut screen found by pinching with thumb and three fingers
How does the math work when buying airline miles?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Finding Raster Pixel Neighbors using GDAL?
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Smoothing/interpolating raster in Python using GDAL?Raster data array output flipped on x-axis using python/gdal?can't generate/display signed float values using python/gdalDo rasters other than “north up” exist in the wild?Conditional assignment of values to adjacent raster cells?Output raster from geostatistical layer to match template raster: why is there an extra row of NoData cells?Changing projection and extent of raster using gdalClipping a raster without pixel displacement / shiftingHow to fix positive North-South Pixel size in GeoTIFF with GDALPostGIS Raster - geocoordinates - where is pixel center
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm new to GDAL Python, and I'm working on a little recursive algorithm which requires finding the neighbors of a a pixel on a DEM raster. I've been able to do this by creating a little PixelLite class which is essentially just a tuple representing the raster coordinates of a pixel. Here's a bit of its implementation (feel free to skip):
# a lighweight version of a pixel for quick computation
class PixelLite:
def __init__(self, x, y, raster_meta):
# set x and y
self.x = x
self.y = y
# set max_x and max_y
self.raster_meta = raster_meta
self.max_x = raster_meta.x_max
self.max_y = raster_meta.y_max
# set error flag
self.flag = False
# the set of all neighbors
self.neighbors = set()
if self.x > self.max_x | self.x < 0 | self.y > self.max_y | self.y < 0: # something has gone terribly wrong
self.flag = True
return
# !!! Below, I'm going to set out of bounds values to the value of the pixel itself, which will later be removed from the set !!!
# neighbor columns
if self.x > 0: # if left value will not be negative
left = self.x - 1 # add legit left
else:
left = self.x # add left value that will be removed
center = self.x
if self.x < self.max_x: # if right value will not be larger than raster dimensions
right = self.x + 1 # add legit right value
else:
right = self.x # add right value that will be removed
# Note: bottom and top are arbitrary names, what matters is that values do no exceed bounds; it may be the case that, because of raster indexing 'bottom' actually represents the pixel above the current pixel
# neighbor columns
if self.y > 0: # if bottom value will not be negative
bottom = self.y - 1 # add legit bottom value
else:
bottom = self.y # add bottom value that will be removed
middle = self.y
if self.y < self.max_y: # if top value will not exceed raster extent
top = self.y + 1 # add legit top value
else:
top = self.y # add top value that will be removed
# top row of neighbors
self.neighbors.add((left, top))
self.neighbors.add((center, top))
self.neighbors.add((right, top))
# middle row of neighbors
self.neighbors.add((left, middle))
self.neighbors.add((right, middle))
# bottom row of neighbors
self.neighbors.add((left, bottom))
self.neighbors.add((center, bottom))
self.neighbors.add((right, bottom))
# the method I'm using will sometimes add the center, middle element as I've used that as a default to set values to if there is an out of bounds error. So let's just pop that off...; a pixel cannot be its own neighbor
self.neighbors.discard((center, middle))
...
Basically, what this does is create the tuples representing each neighbor for a given pixel whenever the PixelLite class is instantiated. Although this is pretty lightweight, because it is implemented in very basic Python without any kind of binding, I would imagine it lacks the optimization of a more sophisticated solution such as I would expect to find in GDAL.
Is there a faster getPixelNeighbors
function provided by GDAL or another raster library i.e. one which would allow me to do the same but which might have some neat little optimization tricks up its sleeve?
raster gdal
New contributor
add a comment |
I'm new to GDAL Python, and I'm working on a little recursive algorithm which requires finding the neighbors of a a pixel on a DEM raster. I've been able to do this by creating a little PixelLite class which is essentially just a tuple representing the raster coordinates of a pixel. Here's a bit of its implementation (feel free to skip):
# a lighweight version of a pixel for quick computation
class PixelLite:
def __init__(self, x, y, raster_meta):
# set x and y
self.x = x
self.y = y
# set max_x and max_y
self.raster_meta = raster_meta
self.max_x = raster_meta.x_max
self.max_y = raster_meta.y_max
# set error flag
self.flag = False
# the set of all neighbors
self.neighbors = set()
if self.x > self.max_x | self.x < 0 | self.y > self.max_y | self.y < 0: # something has gone terribly wrong
self.flag = True
return
# !!! Below, I'm going to set out of bounds values to the value of the pixel itself, which will later be removed from the set !!!
# neighbor columns
if self.x > 0: # if left value will not be negative
left = self.x - 1 # add legit left
else:
left = self.x # add left value that will be removed
center = self.x
if self.x < self.max_x: # if right value will not be larger than raster dimensions
right = self.x + 1 # add legit right value
else:
right = self.x # add right value that will be removed
# Note: bottom and top are arbitrary names, what matters is that values do no exceed bounds; it may be the case that, because of raster indexing 'bottom' actually represents the pixel above the current pixel
# neighbor columns
if self.y > 0: # if bottom value will not be negative
bottom = self.y - 1 # add legit bottom value
else:
bottom = self.y # add bottom value that will be removed
middle = self.y
if self.y < self.max_y: # if top value will not exceed raster extent
top = self.y + 1 # add legit top value
else:
top = self.y # add top value that will be removed
# top row of neighbors
self.neighbors.add((left, top))
self.neighbors.add((center, top))
self.neighbors.add((right, top))
# middle row of neighbors
self.neighbors.add((left, middle))
self.neighbors.add((right, middle))
# bottom row of neighbors
self.neighbors.add((left, bottom))
self.neighbors.add((center, bottom))
self.neighbors.add((right, bottom))
# the method I'm using will sometimes add the center, middle element as I've used that as a default to set values to if there is an out of bounds error. So let's just pop that off...; a pixel cannot be its own neighbor
self.neighbors.discard((center, middle))
...
Basically, what this does is create the tuples representing each neighbor for a given pixel whenever the PixelLite class is instantiated. Although this is pretty lightweight, because it is implemented in very basic Python without any kind of binding, I would imagine it lacks the optimization of a more sophisticated solution such as I would expect to find in GDAL.
Is there a faster getPixelNeighbors
function provided by GDAL or another raster library i.e. one which would allow me to do the same but which might have some neat little optimization tricks up its sleeve?
raster gdal
New contributor
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdalDataset
orRasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has aread
method for reading as a numpy array and anindex
method to transform map coordinates to pixel indexes which you can use with your numpy array.
– user2856
28 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago
add a comment |
I'm new to GDAL Python, and I'm working on a little recursive algorithm which requires finding the neighbors of a a pixel on a DEM raster. I've been able to do this by creating a little PixelLite class which is essentially just a tuple representing the raster coordinates of a pixel. Here's a bit of its implementation (feel free to skip):
# a lighweight version of a pixel for quick computation
class PixelLite:
def __init__(self, x, y, raster_meta):
# set x and y
self.x = x
self.y = y
# set max_x and max_y
self.raster_meta = raster_meta
self.max_x = raster_meta.x_max
self.max_y = raster_meta.y_max
# set error flag
self.flag = False
# the set of all neighbors
self.neighbors = set()
if self.x > self.max_x | self.x < 0 | self.y > self.max_y | self.y < 0: # something has gone terribly wrong
self.flag = True
return
# !!! Below, I'm going to set out of bounds values to the value of the pixel itself, which will later be removed from the set !!!
# neighbor columns
if self.x > 0: # if left value will not be negative
left = self.x - 1 # add legit left
else:
left = self.x # add left value that will be removed
center = self.x
if self.x < self.max_x: # if right value will not be larger than raster dimensions
right = self.x + 1 # add legit right value
else:
right = self.x # add right value that will be removed
# Note: bottom and top are arbitrary names, what matters is that values do no exceed bounds; it may be the case that, because of raster indexing 'bottom' actually represents the pixel above the current pixel
# neighbor columns
if self.y > 0: # if bottom value will not be negative
bottom = self.y - 1 # add legit bottom value
else:
bottom = self.y # add bottom value that will be removed
middle = self.y
if self.y < self.max_y: # if top value will not exceed raster extent
top = self.y + 1 # add legit top value
else:
top = self.y # add top value that will be removed
# top row of neighbors
self.neighbors.add((left, top))
self.neighbors.add((center, top))
self.neighbors.add((right, top))
# middle row of neighbors
self.neighbors.add((left, middle))
self.neighbors.add((right, middle))
# bottom row of neighbors
self.neighbors.add((left, bottom))
self.neighbors.add((center, bottom))
self.neighbors.add((right, bottom))
# the method I'm using will sometimes add the center, middle element as I've used that as a default to set values to if there is an out of bounds error. So let's just pop that off...; a pixel cannot be its own neighbor
self.neighbors.discard((center, middle))
...
Basically, what this does is create the tuples representing each neighbor for a given pixel whenever the PixelLite class is instantiated. Although this is pretty lightweight, because it is implemented in very basic Python without any kind of binding, I would imagine it lacks the optimization of a more sophisticated solution such as I would expect to find in GDAL.
Is there a faster getPixelNeighbors
function provided by GDAL or another raster library i.e. one which would allow me to do the same but which might have some neat little optimization tricks up its sleeve?
raster gdal
New contributor
I'm new to GDAL Python, and I'm working on a little recursive algorithm which requires finding the neighbors of a a pixel on a DEM raster. I've been able to do this by creating a little PixelLite class which is essentially just a tuple representing the raster coordinates of a pixel. Here's a bit of its implementation (feel free to skip):
# a lighweight version of a pixel for quick computation
class PixelLite:
def __init__(self, x, y, raster_meta):
# set x and y
self.x = x
self.y = y
# set max_x and max_y
self.raster_meta = raster_meta
self.max_x = raster_meta.x_max
self.max_y = raster_meta.y_max
# set error flag
self.flag = False
# the set of all neighbors
self.neighbors = set()
if self.x > self.max_x | self.x < 0 | self.y > self.max_y | self.y < 0: # something has gone terribly wrong
self.flag = True
return
# !!! Below, I'm going to set out of bounds values to the value of the pixel itself, which will later be removed from the set !!!
# neighbor columns
if self.x > 0: # if left value will not be negative
left = self.x - 1 # add legit left
else:
left = self.x # add left value that will be removed
center = self.x
if self.x < self.max_x: # if right value will not be larger than raster dimensions
right = self.x + 1 # add legit right value
else:
right = self.x # add right value that will be removed
# Note: bottom and top are arbitrary names, what matters is that values do no exceed bounds; it may be the case that, because of raster indexing 'bottom' actually represents the pixel above the current pixel
# neighbor columns
if self.y > 0: # if bottom value will not be negative
bottom = self.y - 1 # add legit bottom value
else:
bottom = self.y # add bottom value that will be removed
middle = self.y
if self.y < self.max_y: # if top value will not exceed raster extent
top = self.y + 1 # add legit top value
else:
top = self.y # add top value that will be removed
# top row of neighbors
self.neighbors.add((left, top))
self.neighbors.add((center, top))
self.neighbors.add((right, top))
# middle row of neighbors
self.neighbors.add((left, middle))
self.neighbors.add((right, middle))
# bottom row of neighbors
self.neighbors.add((left, bottom))
self.neighbors.add((center, bottom))
self.neighbors.add((right, bottom))
# the method I'm using will sometimes add the center, middle element as I've used that as a default to set values to if there is an out of bounds error. So let's just pop that off...; a pixel cannot be its own neighbor
self.neighbors.discard((center, middle))
...
Basically, what this does is create the tuples representing each neighbor for a given pixel whenever the PixelLite class is instantiated. Although this is pretty lightweight, because it is implemented in very basic Python without any kind of binding, I would imagine it lacks the optimization of a more sophisticated solution such as I would expect to find in GDAL.
Is there a faster getPixelNeighbors
function provided by GDAL or another raster library i.e. one which would allow me to do the same but which might have some neat little optimization tricks up its sleeve?
raster gdal
raster gdal
New contributor
New contributor
edited 3 mins ago
PolyGeo♦
54k1782246
54k1782246
New contributor
asked 39 mins ago
LIAM MLIAM M
1
1
New contributor
New contributor
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdalDataset
orRasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has aread
method for reading as a numpy array and anindex
method to transform map coordinates to pixel indexes which you can use with your numpy array.
– user2856
28 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago
add a comment |
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdalDataset
orRasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has aread
method for reading as a numpy array and anindex
method to transform map coordinates to pixel indexes which you can use with your numpy array.
– user2856
28 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdal
Dataset
or RasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has a read
method for reading as a numpy array and an index
method to transform map coordinates to pixel indexes which you can use with your numpy array.– user2856
28 mins ago
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdal
Dataset
or RasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has a read
method for reading as a numpy array and an index
method to transform map coordinates to pixel indexes which you can use with your numpy array.– user2856
28 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
LIAM M is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f319399%2ffinding-raster-pixel-neighbors-using-gdal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
LIAM M is a new contributor. Be nice, and check out our Code of Conduct.
LIAM M is a new contributor. Be nice, and check out our Code of Conduct.
LIAM M is a new contributor. Be nice, and check out our Code of Conduct.
LIAM M is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f319399%2ffinding-raster-pixel-neighbors-using-gdal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There may be optimised neighbourhood code buried in the GDAL C++ lbrary for use by other function, but I'm not aware of it. I would use the gdal
Dataset
orRasterBand
ReadAsArray
to read as a numpy array and use the power of numpy indexing. Even easier, use rasterio, your open dataset object has aread
method for reading as a numpy array and anindex
method to transform map coordinates to pixel indexes which you can use with your numpy array.– user2856
28 mins ago
@user2856 Thanks! I'm currently using a gdal_array which is purportedly an easier way of interfacing between numpy and gdal. But, if I understand you correctly, it sounds like I might still gain some efficiency by dealing with a numpy array directly. Is that correct?
– LIAM M
11 mins ago