File: random_vector.py

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (111 lines) | stat: -rwxr-xr-x 3,872 bytes parent folder | download
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
#!/usr/bin/python2.7

# Generates random shapefile which may be used for benchmarks 

import os, sys, random, string, math
from osgeo import ogr
from optparse import OptionParser

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" )