|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Generates random shapefile which may be used for benchmarks |
| 4 | + |
| 5 | +import os, sys, random, string, math |
| 6 | +from osgeo import ogr |
| 7 | +from optparse import OptionParser |
| 8 | + |
| 9 | +def error ( msg ): |
| 10 | + print msg |
| 11 | + sys.exit( 1 ) |
| 12 | + |
| 13 | +parser = OptionParser("usage: %prog [options] output") |
| 14 | +parser.add_option("-t", "--type", dest="type", type="choice", choices=("point", "line", "polygon"), default="point", help="Geometry type") |
| 15 | +parser.add_option("-f", "--features", dest="features", type="int", default=1000, help="Number of features") |
| 16 | +parser.add_option("-c", "--coordinates", dest="coordinates", type="int", default=10, help="Number of coordinates per feature (lines and polygons)") |
| 17 | +parser.add_option("-a", "--attributes", dest="attributes", type="int", default=10, help="Number of attributes") |
| 18 | +parser.add_option("-e", "--extent", dest="extent", type="string", default="-180,-90,180,90", help="Extent") |
| 19 | + |
| 20 | +(options, args) = parser.parse_args() |
| 21 | +if len(args) != 1: |
| 22 | + error( "Output file path missing" ) |
| 23 | + |
| 24 | +(minx, miny, maxx, maxy) = map ( float, options.extent.split(",") ) |
| 25 | + |
| 26 | +driverName = "ESRI Shapefile" |
| 27 | +drv = ogr.GetDriverByName( driverName ) |
| 28 | +if drv is None: |
| 29 | + error ( "%s driver not available.\n" % driverName ) |
| 30 | + |
| 31 | +# delete if exists |
| 32 | +try: |
| 33 | + if os.path.exists( args[0] ): |
| 34 | + drv.DeleteDataSource( args[0] ) |
| 35 | +except: |
| 36 | + pass |
| 37 | + |
| 38 | +ds = drv.CreateDataSource( args[0] ) |
| 39 | +if ds is None: |
| 40 | + error( "Creation of output file failed.\n" ) |
| 41 | + |
| 42 | +types = { "point": ogr.wkbPoint, "line": ogr.wkbLineString, "polygon": ogr.wkbPolygon } |
| 43 | + |
| 44 | +lyr = ds.CreateLayer( "out", None, types[options.type] ) |
| 45 | +if lyr is None: |
| 46 | + error ( "Layer creation failed.\n" ) |
| 47 | + |
| 48 | +attrTypes = ( ogr.OFTString, ogr.OFTInteger, ogr.OFTReal ) |
| 49 | +stringWidth = 100 |
| 50 | +for a in range(0,options.attributes): |
| 51 | + attrName = "attr%s" % a |
| 52 | + field_defn = ogr.FieldDefn( attrName, random.choice( attrTypes ) ) |
| 53 | + if field_defn.type == ogr.OFTString: |
| 54 | + field_defn.SetWidth( stringWidth ) |
| 55 | + |
| 56 | + if lyr.CreateField ( field_defn ) != 0: |
| 57 | + error ( "Creating Name field failed.\n" ) |
| 58 | + |
| 59 | +feat_defn = lyr.GetLayerDefn() |
| 60 | +for f in range(options.features): |
| 61 | + feat = ogr.Feature( feat_defn ) |
| 62 | + |
| 63 | + buffer = (maxx-minx)/100 |
| 64 | + if options.type == "point": |
| 65 | + geo = ogr.Geometry( ogr.wkbPoint ) |
| 66 | + x = random.uniform( minx, maxx ) |
| 67 | + y = random.uniform( miny, maxy ) |
| 68 | + geo.SetPoint_2D(0, x, y) |
| 69 | + |
| 70 | + elif options.type == "line": |
| 71 | + geo = ogr.Geometry(ogr.wkbLineString) |
| 72 | + xc = random.uniform( minx+buffer, maxx-buffer ) |
| 73 | + yc = random.uniform( miny+buffer, maxy-buffer ) |
| 74 | + for c in range(options.coordinates): |
| 75 | + a = c * 2 * math.pi / options.coordinates |
| 76 | + r = random.uniform( buffer/10, 9*buffer/10 ) |
| 77 | + x = xc + r * math.sin(a) |
| 78 | + y = yc + r * math.cos(a) |
| 79 | + geo.SetPoint_2D(c, x, y) |
| 80 | + |
| 81 | + elif options.type == "polygon": |
| 82 | + ring = ogr.Geometry(ogr.wkbLinearRing) |
| 83 | + xc = random.uniform( minx+buffer, maxx-buffer ) |
| 84 | + yc = random.uniform( miny+buffer, maxy-buffer ) |
| 85 | + for c in range(options.coordinates): |
| 86 | + a = c * 2 * math.pi / options.coordinates |
| 87 | + r = random.uniform( buffer/10, 9*buffer/10 ) |
| 88 | + x = xc + r * math.sin(a) |
| 89 | + y = yc + r * math.cos(a) |
| 90 | + ring.SetPoint_2D(c, x, y) |
| 91 | + geo = ogr.Geometry(ogr.wkbPolygon) |
| 92 | + geo.AddGeometry ( ring ) |
| 93 | + |
| 94 | + feat.SetGeometry(geo) |
| 95 | + |
| 96 | + for i in range(feat_defn.GetFieldCount()): |
| 97 | + field_defn = feat_defn.GetFieldDefn(i) |
| 98 | + val = None |
| 99 | + limit = 10000000 |
| 100 | + if field_defn.GetType() == ogr.OFTString: |
| 101 | + nChars = random.randint(0,stringWidth) |
| 102 | + val = ''.join(random.choice(string.ascii_letters+ string.digits) for x in range(nChars) ) |
| 103 | + elif field_defn.GetType() == ogr.OFTInteger: |
| 104 | + val = random.randint( -limit, limit ) |
| 105 | + elif field_defn.GetType() == ogr.OFTReal: |
| 106 | + val = random.uniform ( -limit, limit ) |
| 107 | + feat.SetField( field_defn.name, val ) |
| 108 | + |
| 109 | + if lyr.CreateFeature(feat) != 0: |
| 110 | + error ( "Failed to create feature in shapefile.\n" ) |
| 111 | + |
0 commit comments