2018-02-05 13:38:02 +01:00
|
|
|
#!/usr/bin/env python3
|
2013-05-06 10:40:49 +02:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
# Generates random shapefile which may be used for benchmarks
|
2013-05-06 10:40:49 +02:00
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import math
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import sys
|
2024-11-29 14:26:30 +01:00
|
|
|
|
2013-05-06 10:40:49 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
from osgeo import ogr
|
2024-11-29 14:26:30 +01:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
|
|
|
|
def error(msg):
|
|
|
|
print(msg)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2013-05-06 10:40:49 +02:00
|
|
|
|
|
|
|
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",
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
2013-05-06 10:40:49 +02:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
if len(args) != 1:
|
2017-03-04 19:41:23 +01:00
|
|
|
error("Output file path missing")
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
(minx, miny, maxx, maxy) = map(float, options.extent.split(","))
|
2013-05-06 10:40:49 +02:00
|
|
|
|
|
|
|
driverName = "ESRI Shapefile"
|
2017-03-04 19:41:23 +01:00
|
|
|
drv = ogr.GetDriverByName(driverName)
|
2013-05-06 10:40:49 +02:00
|
|
|
if drv is None:
|
2017-03-04 19:41:23 +01:00
|
|
|
error("%s driver not available.\n" % driverName)
|
2013-05-06 10:40:49 +02:00
|
|
|
|
|
|
|
# delete if exists
|
|
|
|
try:
|
2017-03-04 19:41:23 +01:00
|
|
|
if os.path.exists(args[0]):
|
|
|
|
drv.DeleteDataSource(args[0])
|
2013-05-06 10:40:49 +02:00
|
|
|
except:
|
2017-03-04 19:41:23 +01:00
|
|
|
pass
|
|
|
|
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
ds = drv.CreateDataSource(args[0])
|
2013-05-06 10:40:49 +02:00
|
|
|
if ds is None:
|
2017-03-04 19:41:23 +01:00
|
|
|
error("Creation of output file failed.\n")
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
types = {"point": ogr.wkbPoint, "line": ogr.wkbLineString, "polygon": ogr.wkbPolygon}
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
lyr = ds.CreateLayer("out", None, types[options.type])
|
2013-05-06 10:40:49 +02:00
|
|
|
if lyr is None:
|
2017-03-04 19:41:23 +01:00
|
|
|
error("Layer creation failed.\n")
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
attrTypes = (ogr.OFTString, ogr.OFTInteger, ogr.OFTReal)
|
2013-05-06 10:40:49 +02:00
|
|
|
stringWidth = 100
|
2017-03-04 19:41:23 +01:00
|
|
|
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)
|
2013-05-06 10:40:49 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
if lyr.CreateField(field_defn) != 0:
|
|
|
|
error("Creating Name field failed.\n")
|
2013-05-06 10:40:49 +02:00
|
|
|
|
|
|
|
feat_defn = lyr.GetLayerDefn()
|
|
|
|
for f in range(options.features):
|
2017-03-04 19:41:23 +01:00
|
|
|
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)
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
2017-03-04 19:41:23 +01:00
|
|
|
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")
|