Skip to content

Commit 0a2cea9

Browse files
committed
Add randomPoints function to the geom module
1 parent 038c729 commit 0a2cea9

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

geoscript/geom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from compoundring import CompoundRing
1212
from bounds import Bounds
1313
from geom import Geometry
14-
from geom import prepare, simplify, densify, transform, buffer, delaunay, voronoi
14+
from geom import prepare, simplify, densify, transform, buffer, delaunay, voronoi, randomPoints
1515
from io.wkt import readWKT, writeWKT, fromWKT
1616
from io.wkb import readWKB, writeWKB, fromWKB, toWKB
1717
from io.json import writeJSON, readJSON

geoscript/geom/geom.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from org.locationtech.jts.operation.buffer import BufferOp, BufferParameters
1515
from org.geotools.geometry.jts import JTS
1616
from org.geotools.referencing.operation.transform import AffineTransform2D
17+
from org.locationtech.jts.shape.random import RandomPointsBuilder
1718
from geoscript.geom.bounds import Bounds
1819

1920
_factory = GeometryFactory()
@@ -153,6 +154,19 @@ def buffer(g, distance, singleSided=False):
153154

154155
return BufferOp.bufferOp(g, distance, bp)
155156

157+
def randomPoints(g, number):
158+
"""
159+
Create a number of random Points within the given Geometry
160+
161+
*g* The constraining Geometry
162+
163+
*number* The number of Points to create
164+
"""
165+
builder = RandomPointsBuilder(GeometryFactory())
166+
builder.setExtent(g)
167+
builder.numPoints = number
168+
return builder.getGeometry()
169+
156170
def _bounds(g):
157171
return Bounds(env=g.getEnvelopeInternal())
158172

tests/test_geom.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,8 @@ def testSubLine(self):
227227
subLine = line.subLine(0.33, 0.67)
228228
assert "LINESTRING (1156010.153864557 649246.3016361536, 1175115.6870342216 648021.5879714314)" == str(subLine)
229229

230-
230+
def testRandomPoints(self):
231+
polygon = geom.buffer(geom.Point(1153461.34, 649950.30), 100)
232+
pts = geom.randomPoints(polygon, 10)
233+
print(str(pts))
234+
assert pts.getNumGeometries() == 10

0 commit comments

Comments
 (0)