Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Hoek, Steven
lmgeo
Commits
70724167
Commit
70724167
authored
Jun 23, 2016
by
Hoek, Steven
Browse files
method get_transformed_boundary_box defined separately
parent
16b11c9a
Changes
1
Hide whitespace changes
Inline
Side-by-side
toolbox/punchlib.py
View file @
70724167
import
numpy
as
np
import
numpy
as
np
import
lmgeo.toolbox.cliplib
as
cp
import
lmgeo.toolbox.cliplib
as
cp
import
lmgeo.formats.gridenvelope2d
as
ge
import
lmgeo.formats.gridenvelope2d
as
ge
from
pyproj
import
Proj
,
transform
from
pyproj
import
Proj
,
transform
...
@@ -82,7 +82,7 @@ def snap_feature_bounds(bbox, rastergrid):
...
@@ -82,7 +82,7 @@ def snap_feature_bounds(bbox, rastergrid):
result
[
3
]
=
rastergrid
.
yll
+
factor
*
rastergrid
.
dy
result
[
3
]
=
rastergrid
.
yll
+
factor
*
rastergrid
.
dy
return
result
return
result
"""Avoid calling the following method for too large boundary boxes to avoid slow performance"""
"""Avoid calling the following method for too large boundary boxes
,
to avoid slow performance"""
def
get_mask
(
snapped_bbox
,
points
,
cellsize
,
inproj4
=
''
,
outproj4
=
''
):
def
get_mask
(
snapped_bbox
,
points
,
cellsize
,
inproj4
=
''
,
outproj4
=
''
):
# Assume that the cellsize can be derived from the rastergrid
# Assume that the cellsize can be derived from the rastergrid
xll
,
yll
=
snapped_bbox
[
0
],
snapped_bbox
[
1
]
xll
,
yll
=
snapped_bbox
[
0
],
snapped_bbox
[
1
]
...
@@ -130,6 +130,41 @@ def get_field_index(fields, srchfld):
...
@@ -130,6 +130,41 @@ def get_field_index(fields, srchfld):
k
=
k
-
1
# DeletionFlag is always the first field, but is not represented in the records
k
=
k
-
1
# DeletionFlag is always the first field, but is not represented in the records
return
k
return
k
def
get_transformed_boundary_box
(
bbox
,
inproj4
=
''
,
outproj4
=
''
,
rotate
=
True
):
# Initialise
result
=
[]
# Check input
if
(
inproj4
==
''
)
or
(
outproj4
==
''
):
raise
Exception
(
"Proj.4 string cannot be empty!"
)
try
:
inProj
=
Proj
(
inproj4
)
outProj
=
Proj
(
outproj4
)
pt1
=
(
bbox
[
0
],
bbox
[
1
])
# LL
pt2
=
(
bbox
[
0
],
bbox
[
3
])
# UL
pt3
=
(
bbox
[
2
],
bbox
[
1
])
# LR
pt4
=
(
bbox
[
2
],
bbox
[
3
])
# UR
minx
,
miny
=
transform
(
inProj
,
outProj
,
pt1
[
0
],
pt1
[
1
])
maxx
,
maxy
=
minx
,
miny
if
not
rotate
:
result
.
append
((
minx
,
miny
))
for
pt
in
[
pt2
,
pt3
,
pt4
]:
x
,
y
=
transform
(
inProj
,
outProj
,
pt
[
0
],
pt
[
1
])
if
rotate
:
minx
=
min
(
minx
,
x
)
maxx
=
max
(
maxx
,
x
)
miny
=
min
(
miny
,
y
)
maxy
=
max
(
maxy
,
y
)
else
:
result
.
append
((
x
,
y
))
if
rotate
:
result
=
[
minx
,
miny
,
maxx
,
maxy
]
except
Exception
as
e
:
print
"Error in method get_transformed_boundary_box of module punchlib: "
+
str
(
e
)
finally
:
return
result
"""Extra functionality is that the shape and the raster can have different coordinate systems
"""Extra functionality is that the shape and the raster can have different coordinate systems
and the obtained values can be transformed using the supplied function f before analysis,
and the obtained values can be transformed using the supplied function f before analysis,
i.e. in case a table is requested showing a frequency analysis. E.g. if the last digit of the
i.e. in case a table is requested showing a frequency analysis. E.g. if the last digit of the
...
@@ -147,24 +182,12 @@ def get_zonal_stats_record(myshape, rastergrid, stat_types, inproj4='', outproj4
...
@@ -147,24 +182,12 @@ def get_zonal_stats_record(myshape, rastergrid, stat_types, inproj4='', outproj4
try
:
try
:
if
(
inproj4
!=
''
)
and
(
outproj4
!=
''
):
if
(
inproj4
!=
''
)
and
(
outproj4
!=
''
):
# Apparently, the coordinate system is not the same for the raster and the grid
# Apparently, the coordinate system is not the same for the raster and the shape
# Convert all the points in order to be sure we get a proper boundary box and mask
# Convert all the points in order to be sure we get a proper boundary box and mask
bbox
=
get_transformed_boundary_box
(
myshape
.
bbox
)
points
=
[]
inProj
=
Proj
(
inproj4
)
inProj
=
Proj
(
inproj4
)
outProj
=
Proj
(
outproj4
)
outProj
=
Proj
(
outproj4
)
pt1
=
(
myshape
.
bbox
[
0
],
myshape
.
bbox
[
1
])
pt2
=
(
myshape
.
bbox
[
0
],
myshape
.
bbox
[
3
])
pt3
=
(
myshape
.
bbox
[
2
],
myshape
.
bbox
[
1
])
pt4
=
(
myshape
.
bbox
[
2
],
myshape
.
bbox
[
3
])
minx
,
miny
=
transform
(
inProj
,
outProj
,
pt1
[
0
],
pt1
[
1
])
maxx
,
maxy
=
minx
,
miny
for
pt
in
[
pt2
,
pt3
,
pt4
]:
x
,
y
=
transform
(
inProj
,
outProj
,
pt
[
0
],
pt
[
1
])
minx
=
min
(
minx
,
x
)
maxx
=
max
(
maxx
,
x
)
miny
=
min
(
miny
,
y
)
maxy
=
max
(
maxy
,
y
)
bbox
=
[
minx
,
miny
,
maxx
,
maxy
]
points
=
[]
for
pt
in
myshape
.
points
:
for
pt
in
myshape
.
points
:
x
,
y
=
transform
(
inProj
,
outProj
,
pt
[
0
],
pt
[
1
])
x
,
y
=
transform
(
inProj
,
outProj
,
pt
[
0
],
pt
[
1
])
points
.
append
([
x
,
y
])
points
.
append
([
x
,
y
])
...
@@ -224,7 +247,8 @@ def get_zonal_stats_record(myshape, rastergrid, stat_types, inproj4='', outproj4
...
@@ -224,7 +247,8 @@ def get_zonal_stats_record(myshape, rastergrid, stat_types, inproj4='', outproj4
print
str
(
e
)
print
str
(
e
)
finally
:
finally
:
return
result
return
result
"""For each shape we'll tally how many grid cells with values a, b etc. occur within its boundary box"""
def
get_zonal_stats_as_table
(
shapeRecords
,
srchfld
,
rastergrid
,
stat_types
):
def
get_zonal_stats_as_table
(
shapeRecords
,
srchfld
,
rastergrid
,
stat_types
):
result
=
[]
result
=
[]
rec
=
{
srchfld
:
0
}
rec
=
{
srchfld
:
0
}
...
@@ -240,6 +264,9 @@ def get_zonal_stats_as_table(shapeRecords, srchfld, rastergrid, stat_types):
...
@@ -240,6 +264,9 @@ def get_zonal_stats_as_table(shapeRecords, srchfld, rastergrid, stat_types):
result
.
append
(
rec
)
result
.
append
(
rec
)
return
result
return
result
def
get_centroid
(
rastergrid
,
i
,
k
):
result
=
[
rastergrid
.
xll
+
(
k
+
0.5
)
*
rastergrid
.
dx
]
result
.
extend
([
rastergrid
.
yll
+
(
rastergrid
.
nrows
-
i
-
0.5
)
*
rastergrid
.
dy
])
return
result
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment