def extract_contour_bounds(self, data): import ogr shape_file = self.make_contour_bounds_shapefile() format_map = { 'kml': ('KML',[]), 'geojson': ('GeoJSON',[]), 'sqlite': ('SQLite',('SPATIALITE=YES', 'INIT_WITH_EPSG=YES','OGR_SQLITE_SYNCHRONOUS=OFF')), 'shapefile': ('ESRI Shapefile',[]) } source_ds = ogr.GetDriverByName('ESRI Shapefile').Open(shape_file) ogr_format, options = format_map[data['format']] file_name = self.filesystem.path('extracts',format(data['name'])) if os.path.exists(file_name): if os.path.isdir(file_name): self.filesystem.rm_rf(file_name) else: os.remove(file_name) dest_ds = ogr.GetDriverByName( ogr_format ).CopyDataSource(source_ds,file_name, options=options) if os.path.isdir(file_name): from databundles.util import zip_dir fpath = file_name+'.zip' zip_dir(file_name, fpath) return fpath else: return file_name
def extract_incident_shapefile(self, data): import ogr import databundles.library as dl import databundles.geo as dg import random import dateutil.parser path, fpath, drv, options = self.get_extract_name(data) if os.path.exists(fpath): return fpath year = data['year'] source_bundle = self.library.dep('aacrime') l = self.library aa = dg.get_analysis_area(l, geoid=self.config.build.aa_geoid) r = l.find(dl.QueryCommand().identity(id=source_bundle.identity.id_).partition(table='incidents',space=aa.geoid)).pop() source_partition = l.get(r.partition).partition ds = drv.CreateDataSource(path, options=options) srs = ogr.osr.SpatialReference() srs.ImportFromEPSG(4326) # Lat/Long in WGS84 lyr = ds.CreateLayer( "incidents", srs, ogr.wkbPoint ) lyr.CreateField(ogr.FieldDefn( "type", ogr.OFTString )) # 0 fn=ogr.FieldDefn( "time", ogr.OFTString ) # 1 fn.SetWidth(8) lyr.CreateField(fn) lyr.CreateField(ogr.FieldDefn( "hour", ogr.OFTInteger )) # 2 lyr.CreateField(ogr.FieldDefn( "day_time", ogr.OFTInteger )) # 3 fn = ogr.FieldDefn( "date", ogr.OFTString ) # 4 fn.SetWidth(10) lyr.CreateField(fn) fn = ogr.FieldDefn( "desc", ogr.OFTString ) # 5 fn.SetWidth(400) lyr.CreateField(fn) fn = ogr.FieldDefn( "addr", ogr.OFTString ) # 6 fn.SetWidth(100) lyr.CreateField(fn) rnd = .00001 * 100 # Approx 100m for row in source_partition.query(""" select date, time, lat, lon, type , description, address from incidents where CAST(strftime('%Y', date) AS INTEGER) = {year} """.format(year=year)): pt = ogr.Geometry(ogr.wkbPoint) px = row['lon']+random.uniform(-rnd, rnd) py = row['lat']+random.uniform(-rnd, rnd) pt.SetPoint_2D(0, px, py ) feat = ogr.Feature(lyr.GetLayerDefn()) try: hour = dateutil.parser.parse(row['time']).time().hour except: hour = None feat.SetField(0, str(row['type']) ) feat.SetField(1, str(row['time']) ) feat.SetField(2, hour ) feat.SetField(3, None ) feat.SetField(4, str(row['date']) ) feat.SetField(5, str(row['description']) ) feat.SetField(6, str(row['address']) ) feat.SetGeometry(pt) lyr.CreateFeature(feat) feat.Destroy() if data['format'] == 'shapefile': from databundles.util import zip_dir zip_dir(os.path.dirname(path), fpath) return fpath else: return path