def create_merc_elevmap(dest):
    zoomlevel = 8
    limitx1, limity1, limitx2, limity2 = merc_limits(zoomlevel, hd=True)
    tilesizemask = tilesize - 1

    #a,b=mapper.latlon2merc((61,15),zoomlevel)
    #limitx1,limity1=int(a),int(b)

    #limity1=int(19000)
    #limity2=limity1+tilesize*16
    #limitx1=int(35000)
    #limitx2=limitx1+tilesize*16
    #tilesizemask=tilesize-1
    #limitx1&=~tilesizemask
    #limity1&=~tilesizemask
    #limitx2&=~tilesizemask
    #limity2&=~tilesizemask

    assert (limitx1 & tilesizemask) == 0
    assert (limity1 & tilesizemask) == 0
    assert (limitx2 & tilesizemask) == 0
    assert (limity2 & tilesizemask) == 0
    if os.path.exists(dest):
        os.unlink(dest)
    blob = BlobFile(dest,
                    zoomlevel,
                    limitx1,
                    limity1,
                    limitx2,
                    limity2,
                    "w",
                    tilesize=tilesize)
    for by in xrange(limity1, limity2, tilesize):
        for bx in xrange(limitx1, limitx2, tilesize):
            f = StringIO()
            for y in xrange(tilesize):
                for x in xrange(tilesize):
                    mx = bx + x
                    my = by + y
                    latlon = mapper.merc2latlon((mx, my), zoomlevel)
                    elev = int(get_terrain_elev(latlon))
                    #f.write(pack(">h",elev)) #min
                    f.write(pack(">h", elev))  #max
            buf = f.getvalue()
            assert (len(buf) == tilesize * tilesize * 2)
            blob.add_tile(bx, by, buf)
            print "Perc complete: %.1f%%" % (100.0 * (by - limity1) /
                                             float(limity2 - limity1))
    blob.close()
def create_merc_elevmap(dest):
    zoomlevel = 8
    limitx1, limity1, limitx2, limity2 = merc_limits(zoomlevel, hd=True)
    tilesizemask = tilesize - 1

    # a,b=mapper.latlon2merc((61,15),zoomlevel)
    # limitx1,limity1=int(a),int(b)

    # limity1=int(19000)
    # limity2=limity1+tilesize*16
    # limitx1=int(35000)
    # limitx2=limitx1+tilesize*16
    # tilesizemask=tilesize-1
    # limitx1&=~tilesizemask
    # limity1&=~tilesizemask
    # limitx2&=~tilesizemask
    # limity2&=~tilesizemask

    assert (limitx1 & tilesizemask) == 0
    assert (limity1 & tilesizemask) == 0
    assert (limitx2 & tilesizemask) == 0
    assert (limity2 & tilesizemask) == 0
    if os.path.exists(dest):
        os.unlink(dest)
    blob = BlobFile(dest, zoomlevel, limitx1, limity1, limitx2, limity2, "w", tilesize=tilesize)
    for by in xrange(limity1, limity2, tilesize):
        for bx in xrange(limitx1, limitx2, tilesize):
            f = StringIO()
            for y in xrange(tilesize):
                for x in xrange(tilesize):
                    mx = bx + x
                    my = by + y
                    latlon = mapper.merc2latlon((mx, my), zoomlevel)
                    elev = int(get_terrain_elev(latlon))
                    # f.write(pack(">h",elev)) #min
                    f.write(pack(">h", elev))  # max
            buf = f.getvalue()
            assert len(buf) == tilesize * tilesize * 2
            blob.add_tile(bx, by, buf)
            print "Perc complete: %.1f%%" % (100.0 * (by - limity1) / float(limity2 - limity1))
    blob.close()
def refine_merc_elevmap(src, srczoomlevel):
    zoomlevel = srczoomlevel - 1
    limitx1, limity1, limitx2, limity2 = merc_limits(zoomlevel, hd=True)

    tilesizemask = tilesize - 1
    assert (limitx1 & tilesizemask) == 0
    assert (limity1 & tilesizemask) == 0

    srcblob = BlobFile(src + "%d" % (srczoomlevel,), tilesize=tilesize)
    trgfile = src + "%d" % (zoomlevel,)
    if os.path.exists(trgfile):
        os.unlink(trgfile)
    trgblob = BlobFile(trgfile, zoomlevel, limitx1, limity1, limitx2, limity2, "w", tilesize=tilesize)
    for by in xrange(limity1, limity2, tilesize):
        # if (by-limity1)/float(limity2-limity1)>0.2:
        #    break
        for bx in xrange(limitx1, limitx2, tilesize):
            print "Perc complete: %.1f%%" % (100.0 * (by - limity1) / float(limity2 - limity1))
            loout = numpy.zeros((tilesize, tilesize))
            hiout = numpy.zeros((tilesize, tilesize))
            for suby in xrange(2):
                for subx in xrange(2):
                    srcx = 2 * (bx) + subx * tilesize
                    srcy = 2 * (by) + suby * tilesize
                    buf = srcblob.get_tile(srcx, srcy)
                    if buf == None:
                        buf = onebuf
                        print "Using onebuf"
                    # print "Got buf:",md5.md5(buf).hexdigest()
                    # print "got at %d,%d: %d bytes"%(srcx,srcy,len(buf) if buf else 0)
                    assert len(buf) == tilesize * tilesize * 2
                    fi = StringIO(buf)
                    # losub=numpy.zeros((tilesize,tilesize))
                    hisub = numpy.zeros((tilesize, tilesize))
                    for j in xrange(tilesize):
                        for i in xrange(tilesize):
                            # losub[j,i]=unpack(">h",fi.read(2))[0]
                            hisub[j, i] = unpack(">h", fi.read(2))[0]
                    fi.close()
                    del fi
                    tsh = tilesize / 2
                    for j in xrange(tilesize / 2):
                        for i in xrange(tilesize / 2):

                            def s(sub, fn):
                                return fn(sub[2 * j + a, 2 * i + b] for (a, b) in product(xrange(2), xrange(2)))

                            # losum=s(losub,min)
                            hisum = s(hisub, max)
                            # print "losum: %d from %s"%(losum,losub[2*j,2*i])
                            crd = (j + suby * tsh, i + subx * tsh)
                            # losum=suby*500+subx*2000
                            # hisum=suby*100+subx*500
                            # loout[crd]=losum
                            hiout[crd] = hisum
                    # del losub
                    del hisub
            # print "loout:",loout
            # print "hiout:",hiout
            f = StringIO()
            for j in xrange(tilesize):
                for i in xrange(tilesize):
                    # f.write(pack(">h",loout[j,i])) #min
                    f.write(pack(">h", hiout[j, i]))  # max
            trgblob.add_tile(bx, by, f.getvalue())
def refine_merc_elevmap(src, srczoomlevel):
    zoomlevel = srczoomlevel - 1
    limitx1, limity1, limitx2, limity2 = merc_limits(zoomlevel, hd=True)

    tilesizemask = tilesize - 1
    assert (limitx1 & tilesizemask) == 0
    assert (limity1 & tilesizemask) == 0

    srcblob = BlobFile(src + "%d" % (srczoomlevel, ), tilesize=tilesize)
    trgfile = src + "%d" % (zoomlevel, )
    if os.path.exists(trgfile):
        os.unlink(trgfile)
    trgblob = BlobFile(trgfile,
                       zoomlevel,
                       limitx1,
                       limity1,
                       limitx2,
                       limity2,
                       "w",
                       tilesize=tilesize)
    for by in xrange(limity1, limity2, tilesize):
        #if (by-limity1)/float(limity2-limity1)>0.2:
        #    break
        for bx in xrange(limitx1, limitx2, tilesize):
            print "Perc complete: %.1f%%" % (100.0 * (by - limity1) /
                                             float(limity2 - limity1))
            loout = numpy.zeros((tilesize, tilesize))
            hiout = numpy.zeros((tilesize, tilesize))
            for suby in xrange(2):
                for subx in xrange(2):
                    srcx = 2 * (bx) + subx * tilesize
                    srcy = 2 * (by) + suby * tilesize
                    buf = srcblob.get_tile(srcx, srcy)
                    if buf == None:
                        buf = onebuf
                        print "Using onebuf"
                    #print "Got buf:",md5.md5(buf).hexdigest()
                    #print "got at %d,%d: %d bytes"%(srcx,srcy,len(buf) if buf else 0)
                    assert (len(buf) == tilesize * tilesize * 2)
                    fi = StringIO(buf)
                    #losub=numpy.zeros((tilesize,tilesize))
                    hisub = numpy.zeros((tilesize, tilesize))
                    for j in xrange(tilesize):
                        for i in xrange(tilesize):
                            #losub[j,i]=unpack(">h",fi.read(2))[0]
                            hisub[j, i] = unpack(">h", fi.read(2))[0]
                    fi.close()
                    del fi
                    tsh = tilesize / 2
                    for j in xrange(tilesize / 2):
                        for i in xrange(tilesize / 2):

                            def s(sub, fn):
                                return fn(
                                    sub[2 * j + a, 2 * i + b]
                                    for (a,
                                         b) in product(xrange(2), xrange(2)))

                            #losum=s(losub,min)
                            hisum = s(hisub, max)
                            #print "losum: %d from %s"%(losum,losub[2*j,2*i])
                            crd = (j + suby * tsh, i + subx * tsh)
                            #losum=suby*500+subx*2000
                            #hisum=suby*100+subx*500
                            #loout[crd]=losum
                            hiout[crd] = hisum
                    #del losub
                    del hisub
            #print "loout:",loout
            #print "hiout:",hiout
            f = StringIO()
            for j in xrange(tilesize):
                for i in xrange(tilesize):
                    #f.write(pack(">h",loout[j,i])) #min
                    f.write(pack(">h", hiout[j, i]))  #max
            trgblob.add_tile(bx, by, f.getvalue())
Example #5
0
def generate_work_packages(tma,
                           blobs,
                           cachedir,
                           maxzoomlevel=13,
                           meta=0,
                           cutoff=8):
    meta = meta  #Change back to if using mapnik
    #if meta==0:
    #    print "\n\n\n\n\n=====================================================================\nWARNING! meta==0!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"
    packcnt = 0
    for zoomlevel in xrange(maxzoomlevel + 1):
        if zoomlevel <= cutoff:
            lat1, lon1, lat2, lon2 = latlon_limits_hd()
        else:
            lat1, lon1, lat2, lon2 = latlon_limits()

        lat1 = float(lat1)
        lat2 = float(lat2)
        lon1 = float(lon1)
        lon2 = float(lon2)

        maxy = mapper.max_merc_y(zoomlevel)
        maxx = mapper.max_merc_x(zoomlevel)
        hd = False
        if zoomlevel <= cutoff:
            hd = True
        limitx1, limity1, limitx2, limity2 = merc_limits(zoomlevel, hd=hd)
        assert limitx2 > limitx1
        assert limity2 > limity1
        print "Limits: %f,%f - %f,%f" % (limitx1, limity1, limitx2, limity2)
        blobs[zoomlevel] = BlobFile(
            os.path.join(cachedir, "level" + str(zoomlevel)), zoomlevel,
            limitx1, limity1, limitx2, limity2, 'w')

        for my1 in xrange(limity1, limity2, 2048):

            for mx1 in xrange(limitx1, limitx2, 2048):
                already = True
                for i in xrange(0, 2048, 256):
                    for j in xrange(0, 2048, 256):
                        if blobs[zoomlevel].get_tile(mx1 + i, my1 + j) == None:
                            already = False
                            break
                if already:
                    print "Already have %d,%d,%d" % (mx1, my1, zoomlevel)
                    continue
                print "Creating new tile %d,%d,%d (tma=%s)" % (mx1, my1,
                                                               zoomlevel, tma)

                mx2 = mx1 + 2048
                my2 = my1 + 2048
                if my2 > maxy:
                    my2 = maxy
                if mx2 > maxx:
                    mx2 = maxx
                if my1 >= meta:
                    metay1 = meta
                else:
                    metay1 = 0
                if mx1 >= meta:
                    metax1 = meta
                else:
                    metax1 = 0
                if my2 <= maxy - meta:
                    metay2 = meta
                else:
                    metay2 = 0
                if mx2 <= maxx - meta:
                    metax2 = meta
                else:
                    metax2 = 0

                latb, lona = mapper.merc2latlon((mx1, my1), zoomlevel)
                lata, lonb = mapper.merc2latlon((mx2, my2), zoomlevel)
                if latb < lat1: continue
                if lata > lat2: continue
                if lonb < lon1: continue
                if lona > lon2: continue

                coord = (zoomlevel, mx1, my1, mx2, my2)

                packcnt += 1
                yield (coord,
                       dict(checkedout=None,
                            metax1=metax1,
                            metay1=metay1,
                            metax2=metax2,
                            metay2=metay2,
                            render_tma=tma))
    print "Finished initializing work. Created %d work items. Cutoff=%d" % (
        packcnt, cutoff)
Example #6
0
def generate_work_packages(tma,blobs,cachedir,maxzoomlevel=13,meta=0,cutoff=8):
    meta=meta #Change back to if using mapnik
    #if meta==0:
    #    print "\n\n\n\n\n=====================================================================\nWARNING! meta==0!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n"
    packcnt=0
    for zoomlevel in xrange(maxzoomlevel+1):
        if zoomlevel<=cutoff:
            lat1,lon1,lat2,lon2=latlon_limits_hd()
        else:
            lat1,lon1,lat2,lon2=latlon_limits()
        
        lat1=float(lat1)
        lat2=float(lat2)
        lon1=float(lon1)
        lon2=float(lon2)
        
        maxy=mapper.max_merc_y(zoomlevel)
        maxx=mapper.max_merc_x(zoomlevel)
        hd=False
        if zoomlevel<=cutoff:
            hd=True
        limitx1,limity1,limitx2,limity2=merc_limits(zoomlevel,hd=hd)
        assert limitx2>limitx1
        assert limity2>limity1
        print "Limits: %f,%f - %f,%f"%(limitx1,limity1,limitx2,limity2)
        blobs[zoomlevel]=BlobFile(os.path.join(cachedir,"level"+str(zoomlevel)),zoomlevel,
            limitx1,limity1,limitx2,limity2,'w')
            
        for my1 in xrange(limity1,limity2,2048):

            for mx1 in xrange(limitx1,limitx2,2048):
                already=True
                for i in xrange(0,2048,256):
                    for j in xrange(0,2048,256):
                        if blobs[zoomlevel].get_tile(mx1+i,my1+j)==None:
                            already=False
                            break
                if already:
                    print "Already have %d,%d,%d"%(mx1,my1,zoomlevel)
                    continue
                print "Creating new tile %d,%d,%d (tma=%s)"%(mx1,my1,zoomlevel,tma)
                                
                mx2=mx1+2048
                my2=my1+2048
                if my2>maxy:
                    my2=maxy
                if mx2>maxx:
                    mx2=maxx
                if my1>=meta:
                    metay1=meta
                else:
                    metay1=0
                if mx1>=meta:
                    metax1=meta
                else:
                    metax1=0
                if my2<=maxy-meta:
                    metay2=meta
                else:
                    metay2=0
                if mx2<=maxx-meta:
                    metax2=meta
                else:
                    metax2=0
                    
                latb,lona=mapper.merc2latlon((mx1,my1),zoomlevel)
                lata,lonb=mapper.merc2latlon((mx2,my2),zoomlevel)
                if latb<lat1: continue
                if lata>lat2: continue
                if lonb<lon1: continue                
                if lona>lon2: continue                                
                    
                coord=(zoomlevel,mx1,my1,mx2,my2)

                packcnt+=1
                yield (coord,dict(
                           checkedout=None,
                           metax1=metax1,
                           metay1=metay1,
                           metax2=metax2,
                           metay2=metay2,
                           render_tma=tma
                           ))
    print "Finished initializing work. Created %d work items. Cutoff=%d"%(packcnt,cutoff)