[python-geojson] 03/06: Imported Upstream version 1.3.0
Sebastiaan Couwenberg
sebastic at moszumanska.debian.org
Sat Aug 15 20:01:46 UTC 2015
This is an automated email from the git hooks/post-receive script.
sebastic pushed a commit to branch master
in repository python-geojson.
commit 5fbf19b8c29fdebbe8754e2efc095b3ab8d58572
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date: Sat Aug 15 21:49:37 2015 +0200
Imported Upstream version 1.3.0
---
CHANGELOG.rst | 7 +++++
README.rst | 12 +++++++
geojson/utils.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
setup.py | 2 +-
4 files changed, 116 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 41141eb..81095d2 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,6 +1,13 @@
Changes
=======
+1.3.0 (2015-08-11)
+------------------
+
+- Add utility to generate geometries with random data
+
+ - https://github.com/frewsxcv/python-geojson/pull/60
+
1.2.2 (2015-07-13)
------------------
diff --git a/README.rst b/README.rst
index b08b9a0..5f0a370 100644
--- a/README.rst
+++ b/README.rst
@@ -293,6 +293,18 @@ validation
>>> validation['message']
'the "coordinates" member must be a single position'
+generate_random
+~~~~~~~~~~~~~~~
+
+:code:`geojson.utils.generate_random` yields a geometry type with random data
+
+.. code:: python
+
+ >>> import geojson
+
+ >>> geojson.utils.generate_random("LineString") # doctest: +ELLIPSIS
+ {"coordinates": [...], "type": "LineString"}
+
Development
-----------
diff --git a/geojson/utils.py b/geojson/utils.py
index 2639e45..718606f 100644
--- a/geojson/utils.py
+++ b/geojson/utils.py
@@ -54,3 +54,99 @@ def map_coords(func, obj):
else:
raise ValueError("Invalid geometry object %s" % repr(obj))
return {'type': obj['type'], 'coordinates': coordinates}
+
+
+def generate_random(featureType, numberVertices=3,
+ boundingBox=[-180.0, -90.0, 180.0, 90.0]):
+ """
+ Generates random geojson features depending on the parameters
+ passed through.
+
+ :param featureType: A geometry type
+ :type string: Point, LineString, Polygon
+ :param numberVertices: The number vertices that
+ a linestring or polygon will have
+ :type int: defaults to 3
+ :param boundingBox: A bounding box in which features will be restricted to
+ :type list: defaults to the world - [-180.0, -90.0, 180.0, 90.0]
+ :return: The resulting random geojson object or geometry collection.
+ :rtype: object
+ :raises ValueError: if there is no featureType provided.
+ """
+ from geojson import Point, LineString, Polygon
+ import random
+ import math
+
+ lonMin = boundingBox[0]
+ lonMax = boundingBox[2]
+
+ def randomLon():
+ return random.uniform(lonMin, lonMax)
+
+ latMin = boundingBox[1]
+ latMax = boundingBox[3]
+
+ def randomLat():
+ return random.uniform(latMin, latMax)
+
+ def createPoint():
+ return Point((randomLon(), randomLat()))
+
+ def createLine():
+ coords = []
+ for i in range(0, numberVertices):
+ coords.append((randomLon(), randomLat()))
+ return LineString(coords)
+
+ def createPoly():
+ aveRadius = 60
+ ctrX = 0.1
+ ctrY = 0.2
+ irregularity = clip(0.1, 0, 1) * 2 * math.pi / numberVertices
+ spikeyness = clip(0.5, 0, 1) * aveRadius
+
+ angleSteps = []
+ lower = (2 * math.pi / numberVertices) - irregularity
+ upper = (2 * math.pi / numberVertices) + irregularity
+ sum = 0
+ for i in range(numberVertices):
+ tmp = random.uniform(lower, upper)
+ angleSteps.append(tmp)
+ sum = sum + tmp
+
+ k = sum / (2 * math.pi)
+ for i in range(numberVertices):
+ angleSteps[i] = angleSteps[i] / k
+
+ points = []
+ angle = random.uniform(0, 2 * math.pi)
+
+ for i in range(numberVertices):
+ r_i = clip(random.gauss(aveRadius, spikeyness), 0, 2 * aveRadius)
+ x = ctrX + r_i * math.cos(angle)
+ y = ctrY + r_i * math.sin(angle)
+ points.append((int(x), int(y)))
+ angle = angle + angleSteps[i]
+
+ firstVal = points[0]
+ points.append(firstVal)
+ return Polygon([points])
+
+ def clip(x, min, max):
+ if(min > max):
+ return x
+ elif(x < min):
+ return min
+ elif(x > max):
+ return max
+ else:
+ return x
+
+ if featureType == 'Point':
+ return createPoint()
+
+ if featureType == 'LineString':
+ return createLine()
+
+ if featureType == 'Polygon':
+ return createPoly()
diff --git a/setup.py b/setup.py
index 081b6ab..cf3fddd 100644
--- a/setup.py
+++ b/setup.py
@@ -30,7 +30,7 @@ import multiprocessing # NOQA
setup(
name="geojson",
- version="1.2.2",
+ version="1.3.0",
description="Python bindings and utilities for GeoJSON",
license="BSD",
keywords="gis geography json",
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/python-geojson.git
More information about the Pkg-grass-devel
mailing list