Finding distance between point and polygon edge in eastward direction using PyQGIS?
up vote
4
down vote
favorite
I need to find distance in eastward direction from each point to the closest polygon edge:
I can find distance in any direction with:
pointlayer = QgsProject.instance().mapLayersByName('points_inter_squares')[0]
points = [[f.geometry(),f['Ruta_100km']] for f in pointlayer.getFeatures()]
squarelayer = QgsProject.instance().mapLayersByName('squares')[0]
squares = [[f.geometry(),f['Ruta_100km']] for f in squarelayer.getFeatures()]
for point in points:
print(min([QgsGeometry.distance(point[0],square[0]) for square in squares if point[1]!=square[1]]))
>>12639.380321901293
>>3320.150455611874
>>6650.862023710273
>>2452.079442886869
'Ruta_100km' is the Squares IDs, so I measure distance to all Squares but the one intersecting the Points.
Is there a method where I can specify direction when measuring distance?
pyqgis distance
add a comment |
up vote
4
down vote
favorite
I need to find distance in eastward direction from each point to the closest polygon edge:
I can find distance in any direction with:
pointlayer = QgsProject.instance().mapLayersByName('points_inter_squares')[0]
points = [[f.geometry(),f['Ruta_100km']] for f in pointlayer.getFeatures()]
squarelayer = QgsProject.instance().mapLayersByName('squares')[0]
squares = [[f.geometry(),f['Ruta_100km']] for f in squarelayer.getFeatures()]
for point in points:
print(min([QgsGeometry.distance(point[0],square[0]) for square in squares if point[1]!=square[1]]))
>>12639.380321901293
>>3320.150455611874
>>6650.862023710273
>>2452.079442886869
'Ruta_100km' is the Squares IDs, so I measure distance to all Squares but the one intersecting the Points.
Is there a method where I can specify direction when measuring distance?
pyqgis distance
1
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I need to find distance in eastward direction from each point to the closest polygon edge:
I can find distance in any direction with:
pointlayer = QgsProject.instance().mapLayersByName('points_inter_squares')[0]
points = [[f.geometry(),f['Ruta_100km']] for f in pointlayer.getFeatures()]
squarelayer = QgsProject.instance().mapLayersByName('squares')[0]
squares = [[f.geometry(),f['Ruta_100km']] for f in squarelayer.getFeatures()]
for point in points:
print(min([QgsGeometry.distance(point[0],square[0]) for square in squares if point[1]!=square[1]]))
>>12639.380321901293
>>3320.150455611874
>>6650.862023710273
>>2452.079442886869
'Ruta_100km' is the Squares IDs, so I measure distance to all Squares but the one intersecting the Points.
Is there a method where I can specify direction when measuring distance?
pyqgis distance
I need to find distance in eastward direction from each point to the closest polygon edge:
I can find distance in any direction with:
pointlayer = QgsProject.instance().mapLayersByName('points_inter_squares')[0]
points = [[f.geometry(),f['Ruta_100km']] for f in pointlayer.getFeatures()]
squarelayer = QgsProject.instance().mapLayersByName('squares')[0]
squares = [[f.geometry(),f['Ruta_100km']] for f in squarelayer.getFeatures()]
for point in points:
print(min([QgsGeometry.distance(point[0],square[0]) for square in squares if point[1]!=square[1]]))
>>12639.380321901293
>>3320.150455611874
>>6650.862023710273
>>2452.079442886869
'Ruta_100km' is the Squares IDs, so I measure distance to all Squares but the one intersecting the Points.
Is there a method where I can specify direction when measuring distance?
pyqgis distance
pyqgis distance
edited 1 hour ago
Jochen Schwarze
6,09331354
6,09331354
asked 1 hour ago
BERA
14k51839
14k51839
1
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago
add a comment |
1
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago
1
1
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
A general solution: Provided a grid described by it's origin (x0, y0)
and a grid spacing d_grid
(100km in your example), the problem reduces to calculate the difference between the test points p = (x, y)
y-coordinate and the y-coordinate of the grid line lying next to p
, y_square
, so the question is, what is y_square
:
The following does not need a grid layer, but only the description of the grid as described above:
for p in points:
# assumption: grid origin (x0, y0), spacing d_grid
# metric coordinate system
y = p.geometry().asPoint().y()
# calculate y-distance to origin
dy0 = y - y0
# calculate number of square in which p(x, y) lies (2 in the example)
square_count = int(dy0 / d_grid) + 1
# calculate y-coordinate of grid line next to p
y_square = y0 + d_grid * square_count
# calculate the desired distance
dp = y_square - y
print(dp)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
A general solution: Provided a grid described by it's origin (x0, y0)
and a grid spacing d_grid
(100km in your example), the problem reduces to calculate the difference between the test points p = (x, y)
y-coordinate and the y-coordinate of the grid line lying next to p
, y_square
, so the question is, what is y_square
:
The following does not need a grid layer, but only the description of the grid as described above:
for p in points:
# assumption: grid origin (x0, y0), spacing d_grid
# metric coordinate system
y = p.geometry().asPoint().y()
# calculate y-distance to origin
dy0 = y - y0
# calculate number of square in which p(x, y) lies (2 in the example)
square_count = int(dy0 / d_grid) + 1
# calculate y-coordinate of grid line next to p
y_square = y0 + d_grid * square_count
# calculate the desired distance
dp = y_square - y
print(dp)
add a comment |
up vote
2
down vote
A general solution: Provided a grid described by it's origin (x0, y0)
and a grid spacing d_grid
(100km in your example), the problem reduces to calculate the difference between the test points p = (x, y)
y-coordinate and the y-coordinate of the grid line lying next to p
, y_square
, so the question is, what is y_square
:
The following does not need a grid layer, but only the description of the grid as described above:
for p in points:
# assumption: grid origin (x0, y0), spacing d_grid
# metric coordinate system
y = p.geometry().asPoint().y()
# calculate y-distance to origin
dy0 = y - y0
# calculate number of square in which p(x, y) lies (2 in the example)
square_count = int(dy0 / d_grid) + 1
# calculate y-coordinate of grid line next to p
y_square = y0 + d_grid * square_count
# calculate the desired distance
dp = y_square - y
print(dp)
add a comment |
up vote
2
down vote
up vote
2
down vote
A general solution: Provided a grid described by it's origin (x0, y0)
and a grid spacing d_grid
(100km in your example), the problem reduces to calculate the difference between the test points p = (x, y)
y-coordinate and the y-coordinate of the grid line lying next to p
, y_square
, so the question is, what is y_square
:
The following does not need a grid layer, but only the description of the grid as described above:
for p in points:
# assumption: grid origin (x0, y0), spacing d_grid
# metric coordinate system
y = p.geometry().asPoint().y()
# calculate y-distance to origin
dy0 = y - y0
# calculate number of square in which p(x, y) lies (2 in the example)
square_count = int(dy0 / d_grid) + 1
# calculate y-coordinate of grid line next to p
y_square = y0 + d_grid * square_count
# calculate the desired distance
dp = y_square - y
print(dp)
A general solution: Provided a grid described by it's origin (x0, y0)
and a grid spacing d_grid
(100km in your example), the problem reduces to calculate the difference between the test points p = (x, y)
y-coordinate and the y-coordinate of the grid line lying next to p
, y_square
, so the question is, what is y_square
:
The following does not need a grid layer, but only the description of the grid as described above:
for p in points:
# assumption: grid origin (x0, y0), spacing d_grid
# metric coordinate system
y = p.geometry().asPoint().y()
# calculate y-distance to origin
dy0 = y - y0
# calculate number of square in which p(x, y) lies (2 in the example)
square_count = int(dy0 / d_grid) + 1
# calculate y-coordinate of grid line next to p
y_square = y0 + d_grid * square_count
# calculate the desired distance
dp = y_square - y
print(dp)
answered 57 mins ago
Jochen Schwarze
6,09331354
6,09331354
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f305515%2ffinding-distance-between-point-and-polygon-edge-in-eastward-direction-using-pyqg%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
1
You could extend a line from your points in an eastward direction and intersect with the grid, find the segment that touches your creation point and get that segments' length. The eastward segment should be a very large distance to ensure that there is an intersection.. find the extent of your grid and use the maximum X value.
– Michael Stimson
1 hour ago
Is the side length of the squares always 100km and the origin coordinates of the grid an integer multiple of 100km or are you looking for a general solution?
– Jochen Schwarze
1 hour ago
The grid is not always 100 km, but always a square grid.
– BERA
1 hour ago
@Michael Stimson: Isn't there always an intersection if the length of your proposed eastward line equals the grid spacing and the point is not exactly on a grid line?
– Jochen Schwarze
36 mins ago