-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_vector.py
executable file
·159 lines (133 loc) · 4.14 KB
/
random_vector.py
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
153
154
155
156
157
158
159
#!/usr/bin/env python3
# Generates random shapefile which may be used for benchmarks
import math
import os
import random
import string
import sys
from optparse import OptionParser
from osgeo import ogr
def error(msg):
print(msg)
sys.exit(1)
parser = OptionParser("usage: %prog [options] output")
parser.add_option(
"-t",
"--type",
dest="type",
type="choice",
choices=("point", "line", "polygon"),
default="point",
help="Geometry type",
)
parser.add_option(
"-f",
"--features",
dest="features",
type="int",
default=1000,
help="Number of features",
)
parser.add_option(
"-c",
"--coordinates",
dest="coordinates",
type="int",
default=10,
help="Number of coordinates per feature (lines and polygons)",
)
parser.add_option(
"-a",
"--attributes",
dest="attributes",
type="int",
default=10,
help="Number of attributes",
)
parser.add_option(
"-e",
"--extent",
dest="extent",
type="string",
default="-180,-90,180,90",
help="Extent",
)
(options, args) = parser.parse_args()
if len(args) != 1:
error("Output file path missing")
(minx, miny, maxx, maxy) = map(float, options.extent.split(","))
driverName = "ESRI Shapefile"
drv = ogr.GetDriverByName(driverName)
if drv is None:
error("%s driver not available.\n" % driverName)
# delete if exists
try:
if os.path.exists(args[0]):
drv.DeleteDataSource(args[0])
except:
pass
ds = drv.CreateDataSource(args[0])
if ds is None:
error("Creation of output file failed.\n")
types = {"point": ogr.wkbPoint, "line": ogr.wkbLineString, "polygon": ogr.wkbPolygon}
lyr = ds.CreateLayer("out", None, types[options.type])
if lyr is None:
error("Layer creation failed.\n")
attrTypes = (ogr.OFTString, ogr.OFTInteger, ogr.OFTReal)
stringWidth = 100
for a in range(0, options.attributes):
attrName = "attr%s" % a
field_defn = ogr.FieldDefn(attrName, random.choice(attrTypes))
if field_defn.type == ogr.OFTString:
field_defn.SetWidth(stringWidth)
if lyr.CreateField(field_defn) != 0:
error("Creating Name field failed.\n")
feat_defn = lyr.GetLayerDefn()
for f in range(options.features):
feat = ogr.Feature(feat_defn)
buffer = (maxx - minx) / 100
if options.type == "point":
geo = ogr.Geometry(ogr.wkbPoint)
x = random.uniform(minx, maxx)
y = random.uniform(miny, maxy)
geo.SetPoint_2D(0, x, y)
elif options.type == "line":
geo = ogr.Geometry(ogr.wkbLineString)
xc = random.uniform(minx + buffer, maxx - buffer)
yc = random.uniform(miny + buffer, maxy - buffer)
for c in range(options.coordinates):
a = c * 2 * math.pi / options.coordinates
r = random.uniform(buffer / 10, 9 * buffer / 10)
x = xc + r * math.sin(a)
y = yc + r * math.cos(a)
geo.SetPoint_2D(c, x, y)
elif options.type == "polygon":
ring = ogr.Geometry(ogr.wkbLinearRing)
xc = random.uniform(minx + buffer, maxx - buffer)
yc = random.uniform(miny + buffer, maxy - buffer)
for c in range(options.coordinates):
a = c * 2 * math.pi / options.coordinates
r = random.uniform(buffer / 10, 9 * buffer / 10)
x = xc + r * math.sin(a)
y = yc + r * math.cos(a)
ring.SetPoint_2D(c, x, y)
geo = ogr.Geometry(ogr.wkbPolygon)
geo.AddGeometry(ring)
feat.SetGeometry(geo)
for i in range(feat_defn.GetFieldCount()):
field_defn = feat_defn.GetFieldDefn(i)
val = None
limit = 10000000
if field_defn.GetType() == ogr.OFTString:
nChars = random.randint(0, stringWidth)
val = "".join(
random.choice(string.ascii_letters + string.digits)
for x in range(nChars)
)
elif field_defn.GetType() == ogr.OFTInteger:
val = random.randint(-limit, limit)
elif field_defn.GetType() == ogr.OFTReal:
val = random.uniform(-limit, limit)
feat.SetField(field_defn.name, val)
if lyr.CreateFeature(feat) != 0:
error("Failed to create feature in shapefile.\n")