def generate( state, stateFilename, countyFilename, path, zoom ): global geo, scaleoffset print '----------------------------------------' print 'Generating %s %s zoom %d' %( stateFilename, countyFilename, zoom ) scale = 10 geo = Geo( zoom, 256*scale ) pixgeo = Geo( zoom, 256 ) stateShapefile = loadshapefile( stateFilename ) t1 = time.time() stateFeatures = stateShapefile['features'] print '%d state features' % len(stateFeatures) stateFeatures = filterCONUS( stateFeatures ) print '%d features in CONUS states' % len(stateFeatures) #writeFile( 'features.csv', shpUtils.dumpFeatureInfo(features) ) #outer = pixgeo.pixFromGeoBounds( featuresBounds(features) ) fb = featuresBounds( stateFeatures ) outer = pixgeo.pixFromGeoBounds( fb ) outer = pixgeo.inflateBounds( outer, 8 ) gridoffset, gridsize = pixgeo.tileBounds( outer ) scaleoffset = pixgeo.scalePoint( gridoffset, scale ) print 'Offset:[%d,%d], Size:[%d,%d]' %( gridoffset[0], gridoffset[1], gridsize[0], gridsize[1] ) draw = [ 'scale .1,.1\n' ] if countyFilename: countyShapefile = loadshapefile( countyFilename ) countyFeatures = countyShapefile['features'] print '%d county features' % len(countyFeatures) countyFeatures = filterCONUS( countyFeatures ) print '%d features in CONUS counties' % len(countyFeatures) draw.append( 'stroke-width 10\n' ) drawFeatures( draw, countyFeatures, getRandomColor ) draw.append( 'stroke-width 20\n' ) drawFeatures( draw, stateFeatures, None ) else: draw.append( 'stroke-width 10\n' ) drawFeatures( draw, stateFeatures, getRandomColor ) writeFile( 'draw.cmd', ''.join(draw) ) t2 = time.time() print '%0.3f seconds to generate commands' %( t2 - t1 ) crop = True if crop: cropcmd = '-crop 256x256' else: cropcmd = '' blank = magick.blank( gridsize ) base = '%s/tile-%d' %( path, zoom ) command = ( '%s -draw "@draw.cmd" %s ' + base + '.png' )%( blank, cropcmd ) #command = ( '%s -draw "@draw.cmd" %s -depth 8 -type Palette -floodfill 0x0 white -background white -transparent-color white ' + base + '.png' )%( blank, cropcmd ) #command = ( 'null: -resize %dx%d! -floodfill 0x0 white -draw "@draw.cmd" %s -depth 8 -type Palette -background white -transparent white -transparent-color white ' + base + '.png' )%( gridsize[0], gridsize[1], cropcmd ) #command = 'null: -resize %(cx)dx%(cy)d! -draw "@draw.cmd" %(crop)s tile%(zoom)d.png' %({ # 'cx': gridsize[0], # 'cy': gridsize[1], # 'crop': crop, # 'zoom': zoom #}) magick.convert( command ) if crop: xyCount = 2 << zoom n = 0 # TODO: refactor xMin = gridoffset[0] / 256 xMinEdge = max( xMin - 2, 0 ) yMin = gridoffset[1] / 256 yMinEdge = max( yMin - 2, 0 ) xN = gridsize[0] / 256 yN = gridsize[1] /256 xLim = xMin + xN xLimEdge = min( xLim + 2, xyCount ) yLim = yMin + yN yLimEdge = min( yLim + 2, xyCount ) nMoving = xN * yN nCopying = ( xLimEdge - xMinEdge ) * ( yLimEdge - yMinEdge ) - nMoving print 'Moving %d tiles, copying %d blank tiles...' %( nMoving, nCopying ) t1 = time.time() for y in xrange( yMinEdge, yLimEdge ): for x in xrange( xMinEdge, xLimEdge ): target = '%s-%d-%d.png' %( base, y, x ) if xMin <= x < xLim and yMin <= y < yLim: if xN == 1 and yN == 1: source = '%s.png' %( base ) else: source = '%s-%d.png' %( base, n ) if os.path.exists( target ): os.remove( target ) if os.stat(source)[stat.ST_SIZE] > 415: os.rename( source, target ) else: os.remove( source ) shutil.copy( 'blanktile.png', target ) n += 1 else: shutil.copy( 'blanktile.png', target ) t2 = time.time() print '%0.3f seconds to move files' %( t2 - t1 )
def generate(state, stateFilename, countyFilename, path, zoom): global geo, scaleoffset print '----------------------------------------' print 'Generating %s %s zoom %d' % (stateFilename, countyFilename, zoom) scale = 10 geo = Geo(zoom, 256 * scale) pixgeo = Geo(zoom, 256) stateShapefile = loadshapefile(stateFilename) t1 = time.time() stateFeatures = stateShapefile['features'] print '%d state features' % len(stateFeatures) stateFeatures = filterCONUS(stateFeatures) print '%d features in CONUS states' % len(stateFeatures) #writeFile( 'features.csv', shpUtils.dumpFeatureInfo(features) ) #outer = pixgeo.pixFromGeoBounds( featuresBounds(features) ) fb = featuresBounds(stateFeatures) outer = pixgeo.pixFromGeoBounds(fb) outer = pixgeo.inflateBounds(outer, 8) gridoffset, gridsize = pixgeo.tileBounds(outer) scaleoffset = pixgeo.scalePoint(gridoffset, scale) print 'Offset:[%d,%d], Size:[%d,%d]' % (gridoffset[0], gridoffset[1], gridsize[0], gridsize[1]) draw = ['scale .1,.1\n'] if countyFilename: countyShapefile = loadshapefile(countyFilename) countyFeatures = countyShapefile['features'] print '%d county features' % len(countyFeatures) countyFeatures = filterCONUS(countyFeatures) print '%d features in CONUS counties' % len(countyFeatures) draw.append('stroke-width 10\n') drawFeatures(draw, countyFeatures, getRandomColor) draw.append('stroke-width 20\n') drawFeatures(draw, stateFeatures, None) else: draw.append('stroke-width 10\n') drawFeatures(draw, stateFeatures, getRandomColor) writeFile('draw.cmd', ''.join(draw)) t2 = time.time() print '%0.3f seconds to generate commands' % (t2 - t1) crop = True if crop: cropcmd = '-crop 256x256' else: cropcmd = '' blank = magick.blank(gridsize) base = '%s/tile-%d' % (path, zoom) command = ('%s -draw "@draw.cmd" %s ' + base + '.png') % (blank, cropcmd) #command = ( '%s -draw "@draw.cmd" %s -depth 8 -type Palette -floodfill 0x0 white -background white -transparent-color white ' + base + '.png' )%( blank, cropcmd ) #command = ( 'null: -resize %dx%d! -floodfill 0x0 white -draw "@draw.cmd" %s -depth 8 -type Palette -background white -transparent white -transparent-color white ' + base + '.png' )%( gridsize[0], gridsize[1], cropcmd ) #command = 'null: -resize %(cx)dx%(cy)d! -draw "@draw.cmd" %(crop)s tile%(zoom)d.png' %({ # 'cx': gridsize[0], # 'cy': gridsize[1], # 'crop': crop, # 'zoom': zoom #}) magick.convert(command) if crop: xyCount = 2 << zoom n = 0 # TODO: refactor xMin = gridoffset[0] / 256 xMinEdge = max(xMin - 2, 0) yMin = gridoffset[1] / 256 yMinEdge = max(yMin - 2, 0) xN = gridsize[0] / 256 yN = gridsize[1] / 256 xLim = xMin + xN xLimEdge = min(xLim + 2, xyCount) yLim = yMin + yN yLimEdge = min(yLim + 2, xyCount) nMoving = xN * yN nCopying = (xLimEdge - xMinEdge) * (yLimEdge - yMinEdge) - nMoving print 'Moving %d tiles, copying %d blank tiles...' % (nMoving, nCopying) t1 = time.time() for y in xrange(yMinEdge, yLimEdge): for x in xrange(xMinEdge, xLimEdge): target = '%s-%d-%d.png' % (base, y, x) if xMin <= x < xLim and yMin <= y < yLim: if xN == 1 and yN == 1: source = '%s.png' % (base) else: source = '%s-%d.png' % (base, n) if os.path.exists(target): os.remove(target) if os.stat(source)[stat.ST_SIZE] > 415: os.rename(source, target) else: os.remove(source) shutil.copy('blanktile.png', target) n += 1 else: shutil.copy('blanktile.png', target) t2 = time.time() print '%0.3f seconds to move files' % (t2 - t1)
def generate(state, zoom): global geo, scaleoffset print '----------------------------------------' print 'Generating %s zoom %d' % (state, zoom) scale = 10 geo = Geo(zoom, 256 * scale) pixgeo = Geo(zoom, 256) #exec re.sub( '.+\(', 'data = (', readFile( '%s/%s.js' %( shapespath, state ) ) ) json = readFile('%s/%s.js' % (shapespath, state)) exec re.sub('^.+\(', 'data = (', json) places = data['places'] #t1 = time.time() places = filterCONUS(places) #outer = pixgeo.pixFromGeoBounds( featuresBounds(features) ) bounds = placesBounds(places) outer = pixgeo.pixFromGeoBounds(bounds) outer = pixgeo.inflateBounds(outer, 8) gridoffset, gridsize = pixgeo.tileBounds(outer) scaleoffset = pixgeo.scalePoint(gridoffset, scale) print 'Offset:[%d,%d], Size:[%d,%d]' % (gridoffset[0], gridoffset[1], gridsize[0], gridsize[1]) draw = ['scale .1,.1\n'] draw.append('stroke-width 10\n') drawPlaces(draw, places) cmdfile = 'draw.tmp' writeFile(cmdfile, ''.join(draw)) #t2 = time.time() #print '%0.3f seconds to generate commands' %( t2 - t1 ) crop = True if crop: cropcmd = '-crop 256x256' else: cropcmd = '' blank = magick.blank(gridsize) base = '%s/%s/%s-%d' % (tilespath, state, state, zoom) command = ('%s -draw "@%s" %s ' + base + '.png') % (blank, cmdfile, cropcmd) #command = ( '%s -draw "@draw.cmd" %s -depth 8 -type Palette -floodfill 0x0 white -background white -transparent-color white ' + base + '.png' )%( blank, cropcmd ) #command = ( 'null: -resize %dx%d! -floodfill 0x0 white -draw "@draw.cmd" %s -depth 8 -type Palette -background white -transparent white -transparent-color white ' + base + '.png' )%( gridsize[0], gridsize[1], cropcmd ) #command = 'null: -resize %(cx)dx%(cy)d! -draw "@draw.cmd" %(crop)s tile%(zoom)d.png' %({ # 'cx': gridsize[0], # 'cy': gridsize[1], # 'crop': crop, # 'zoom': zoom #}) magick.convert(command) if crop: xyCount = 2 << zoom n = 0 # TODO: refactor xMin = gridoffset[0] / 256 xMinEdge = max(xMin - 2, 0) yMin = gridoffset[1] / 256 yMinEdge = max(yMin - 2, 0) xN = gridsize[0] / 256 yN = gridsize[1] / 256 xLim = xMin + xN xLimEdge = min(xLim + 2, xyCount) yLim = yMin + yN yLimEdge = min(yLim + 2, xyCount) nMoving = xN * yN nCopying = (xLimEdge - xMinEdge) * (yLimEdge - yMinEdge) - nMoving print 'Moving %d tiles, copying %d blank tiles...' % (nMoving, nCopying) t1 = time.time() for y in xrange(yMinEdge, yLimEdge): for x in xrange(xMinEdge, xLimEdge): target = '%s-%d-%d.png' % (base, y, x) if xMin <= x < xLim and yMin <= y < yLim: if xN == 1 and yN == 1: source = '%s.png' % (base) else: source = '%s-%d.png' % (base, n) if os.path.exists(target): os.remove(target) if os.stat(source)[stat.ST_SIZE] > 415: os.rename(source, target) else: os.remove(source) shutil.copy('blanktile.png', target) n += 1 else: shutil.copy('blanktile.png', target) t2 = time.time() print '%0.3f seconds to move files' % (t2 - t1)
def generate( state, zoom ): global geo, scaleoffset print '----------------------------------------' print 'Generating %s zoom %d' %( state, zoom ) scale = 10 geo = Geo( zoom, 256*scale ) pixgeo = Geo( zoom, 256 ) #exec re.sub( '.+\(', 'data = (', readFile( '%s/%s.js' %( shapespath, state ) ) ) json = readFile( '%s/%s.js' %( shapespath, state ) ) exec re.sub( '^.+\(', 'data = (', json ) places = data['places'] #t1 = time.time() places = filterCONUS( places ) #outer = pixgeo.pixFromGeoBounds( featuresBounds(features) ) bounds = placesBounds( places ) outer = pixgeo.pixFromGeoBounds( bounds ) outer = pixgeo.inflateBounds( outer, 8 ) gridoffset, gridsize = pixgeo.tileBounds( outer ) scaleoffset = pixgeo.scalePoint( gridoffset, scale ) print 'Offset:[%d,%d], Size:[%d,%d]' %( gridoffset[0], gridoffset[1], gridsize[0], gridsize[1] ) draw = [ 'scale .1,.1\n' ] draw.append( 'stroke-width 10\n' ) drawPlaces( draw, places ) cmdfile = 'draw.tmp' writeFile( cmdfile, ''.join(draw) ) #t2 = time.time() #print '%0.3f seconds to generate commands' %( t2 - t1 ) crop = True if crop: cropcmd = '-crop 256x256' else: cropcmd = '' blank = magick.blank( gridsize ) base = '%s/%s/%s-%d' %( tilespath, state, state, zoom ) command = ( '%s -draw "@%s" %s ' + base + '.png' )%( blank, cmdfile, cropcmd ) #command = ( '%s -draw "@draw.cmd" %s -depth 8 -type Palette -floodfill 0x0 white -background white -transparent-color white ' + base + '.png' )%( blank, cropcmd ) #command = ( 'null: -resize %dx%d! -floodfill 0x0 white -draw "@draw.cmd" %s -depth 8 -type Palette -background white -transparent white -transparent-color white ' + base + '.png' )%( gridsize[0], gridsize[1], cropcmd ) #command = 'null: -resize %(cx)dx%(cy)d! -draw "@draw.cmd" %(crop)s tile%(zoom)d.png' %({ # 'cx': gridsize[0], # 'cy': gridsize[1], # 'crop': crop, # 'zoom': zoom #}) magick.convert( command ) if crop: xyCount = 2 << zoom n = 0 # TODO: refactor xMin = gridoffset[0] / 256 xMinEdge = max( xMin - 2, 0 ) yMin = gridoffset[1] / 256 yMinEdge = max( yMin - 2, 0 ) xN = gridsize[0] / 256 yN = gridsize[1] /256 xLim = xMin + xN xLimEdge = min( xLim + 2, xyCount ) yLim = yMin + yN yLimEdge = min( yLim + 2, xyCount ) nMoving = xN * yN nCopying = ( xLimEdge - xMinEdge ) * ( yLimEdge - yMinEdge ) - nMoving print 'Moving %d tiles, copying %d blank tiles...' %( nMoving, nCopying ) t1 = time.time() for y in xrange( yMinEdge, yLimEdge ): for x in xrange( xMinEdge, xLimEdge ): target = '%s-%d-%d.png' %( base, y, x ) if xMin <= x < xLim and yMin <= y < yLim: if xN == 1 and yN == 1: source = '%s.png' %( base ) else: source = '%s-%d.png' %( base, n ) if os.path.exists( target ): os.remove( target ) if os.stat(source)[stat.ST_SIZE] > 415: os.rename( source, target ) else: os.remove( source ) shutil.copy( 'blanktile.png', target ) n += 1 else: shutil.copy( 'blanktile.png', target ) t2 = time.time() print '%0.3f seconds to move files' %( t2 - t1 )