/usr/lib/python2.7/dist-packages/geojson/utils.py is in python-geojson 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | """Coordinate utility functions."""
def coords(obj):
"""
Yields the coordinates from a Feature or Geometry.
:param obj: A geometry or feature to extract the coordinates from."
:type obj: Feature, Geometry
:return: A generator with coordinate tuples from the geometry or feature.
:rtype: generator
"""
if isinstance(obj, (tuple, list)):
coordinates = obj
elif 'geometry' in obj:
coordinates = obj['geometry']['coordinates']
else:
coordinates = obj.get('coordinates', obj)
for e in coordinates:
if isinstance(e, (float, int)):
yield tuple(coordinates)
break
for f in coords(e):
yield f
def map_coords(func, obj):
"""
Returns the coordinates from a Geometry after applying the provided
function to the tuples.
:param obj: A geometry or feature to extract the coordinates from.
:type obj: Point, LineString, MultiPoint, MultiLineString, Polygon,
MultiPolygon
:return: The result of applying the function to each coordinate array.
:rtype: list
:raises ValueError: if the provided object is not a Geometry.
"""
if obj['type'] == 'Point':
coordinates = tuple(map(func, obj['coordinates']))
elif obj['type'] in ['LineString', 'MultiPoint']:
coordinates = [tuple(map(func, c)) for c in obj['coordinates']]
elif obj['type'] in ['MultiLineString', 'Polygon']:
coordinates = [[
tuple(map(func, c)) for c in curve]
for curve in obj['coordinates']]
elif obj['type'] == 'MultiPolygon':
coordinates = [[[
tuple(map(func, c)) for c in curve]
for curve in part]
for part in obj['coordinates']]
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()
|