コード例 #1
0
ファイル: tile_server.py プロジェクト: wsw2008new/tfm
class OsmApi:
    def __init__(self):
        self.client = Connection(host="mongomaster")
        self.proj = GlobalMercator()

    def getTile(self, zoom, x, y):
        (x, y) = self.proj.GoogleTile(x, y, zoom)
        quadkey = self.proj.QuadTree(x, y, zoom)
        print "Querying for %s." % (quadkey, )
        (minlat, minlon, maxlat,
         maxlon) = self.proj.TileLatLonBounds(x, y, zoom)

        # Nodes in the tile
        nodes = {}
        cursor = self.client.osm.nodes.find(
            {'qk': {
                '$regex': "^%s" % (quadkey, )
            }})
        for row in cursor:
            nodes[row['_id']] = row

        # Ways with nodes in the tile
        ways = {}
        cursor = self.client.osm.ways.find(
            {'loc': {
                '$regex': "^%s" % (quadkey, )
            }})
        for row in cursor:
            ways[row['_id']] = row

        # Nodes on ways that extend beyond the bounding box
        otherNids = set()
        for way in ways.values():
            for nid in way['nodes']:
                otherNids.add(nid)
        cursor = self.client.osm.nodes.find({'_id': {'$in': list(otherNids)}})
        for row in cursor:
            nodes[row['_id']] = row

        # Relations that contain any of the above as members
        relations = {}

        # Sort the results by id
        nodes = sorted(nodes.iteritems())
        ways = sorted(ways.iteritems())
        relations = sorted(relations.iteritems())

        doc = {
            'bounds': {
                'minlat': minlat,
                'minlon': minlon,
                'maxlat': maxlat,
                'maxlon': maxlon
            },
            'nodes': nodes,
            'ways': ways,
            'relations': relations
        }

        return doc
コード例 #2
0
ファイル: dotmap.py プロジェクト: sdwfrost/DotMapper
def main(tiles_path, db_file, groups, zoom_levels):
    merc = GlobalMercator()

    # Set-up the output db
    
    conn = sqlite3.connect(db_file)
    c = conn.cursor()

    for zoom in [zoom_levels]: #TODO zoom levels
        results_set = c.execute("select x, y, quadkey, group_type from people_by_group order by quadkey asc, rand asc" )
        use_ellipse, radius_rel, gamma, os_scale = STYLE[zoom]
        radius = os_scale*radius_rel/4/2
        quadkey = None
        img = None

        for i,r in enumerate(results_set):
            if (i % 1000 == 0):
                print i
    
            x = float(r[0])
            y = float(r[1])
            next_quadkey = r[2][:zoom]
            group = r[3]
    
            if next_quadkey != quadkey:
                #finish last tile
                if img:
                    save_tile(img, tiles_path, zoom, gtx, gty)
                
                quadkey = next_quadkey
                tx, ty = merc.MetersToTile(x, y, zoom)
                gtx, gty = merc.GoogleTile(tx,ty,zoom)
        
                img = Image.new("RGB", (TILE_X*os_scale, TILE_Y*os_scale), "white")
                draw = ImageDraw.Draw(img)
                
            minx, miny, maxx, maxy = (c/A for c in merc.TileBounds(tx, ty, zoom))
            xscale = (TILE_X*os_scale)/(maxx - minx)
            yscale = (TILE_Y*os_scale)/(maxy - miny)


            #print 'minx', minx, 'miny', miny, 'maxx', maxx, 'maxy', maxy
            #print 'xscale',xscale,'yscale',yscale
            #print 'x',x,'y',y,'tx',tx,'ty',ty
        
            # Translate coordinates to tile-relative, google ready coordinates
            rx = (x/A - minx)*xscale
            ry = (maxy - y/A)*yscale
    
            fill=ImageColor.getrgb(groups[group]['color'])
            if use_ellipse:
                draw.ellipse((rx-radius,ry-radius,rx+radius,ry+radius), fill=fill)
            else:
                draw.point((rx, ry), fill=fill)
            #print "Draw at ", (rx-radius,ry-radius,rx+radius,ry+radius), ImageColor.getrgb(groups[group]['color'])

        save_tile(img, tiles_path, zoom, gtx, gty)
    
    save_defined_tiles(tiles_path)
コード例 #3
0
def GetGridID(Coord):
  lat=Coord[0]/1000
  lon=Coord[1]/1000

  tz=8

  mercator = GlobalMercator()
  mx, my = mercator.LatLonToMeters( Coord[0]/1000.0, Coord[1]/1000.0 )
  tx, ty = mercator.MetersToTile( mx, my, tz )

  gx, gy = mercator.GoogleTile(tx, ty, tz)
	#print "\tGoogle:", gx, gy

  #print tx, ty

  return ("%03d" % gx)+("%03d" % gy)
コード例 #4
0
    pbar = ProgressBar(widgets=widgets, maxval=total_tiles).start()

    tile_list = []
    for ty in range(tminy, tmaxy + 1):
        for tx in range(tminx, tmaxx + 1):
            tile_list.append([tx, ty])

    print("Downloading images ...")
    nthreads = multiprocessing.cpu_count() * 2
    pool = multiprocessing.Pool(processes=nthreads)
    for i, _ in enumerate(pool.imap_unordered(process_tile, tile_list), 1):
        pbar.update(i)

    print("merging images ...")
    nthreads = 1
    gtx0, gty0 = mercator.GoogleTile(tminx, tminy, tz)
    debug_print("create image of size: %d, %d" %
                ((tmaxx - tminx + 1) * actual_tile_size,
                 (tmaxy - tminy + 1) * actual_tile_size))
    new_im = Image.new('RGB', ((tmaxx - tminx + 1) * actual_tile_size,
                               (tmaxy - tminy + 1) * actual_tile_size))
    for tile_pair in tile_list:
        merge_images(tile_pair)

    print("saving image ...")
    new_im.save(output_jpeg_file, "JPEG")

    if tif_output:
        # to geotif
        lefttop_minlat, lefttop_minlon, lefttop_maxlat, lefttop_maxlon = mercator.TileLatLonBounds(
            tminx, tminy, tz)
コード例 #5
0
 def getTile(self, zoomlevel):
     mercator = GlobalMercator()
     mx, my = mercator.LatLonToMeters(self.lat, self.lon)
     tminx, tminy = mercator.MetersToTile(mx, my, zoomlevel)
     gx, gy = mercator.GoogleTile(tminx, tminy, zoomlevel)  #+1?
     return gx, gy, zoomlevel
コード例 #6
0
ファイル: tile_grab2.py プロジェクト: geobabbler/tile-grab
root = options.destination + '/' + str(z)
try:
    if os.path.exists(root) == False:
        os.makedirs(root)
except:
    print "Could not create destination. It may already exist."

for x in range(xarr[0], xarr[1] + 1):
    for y in range(yarr[0], yarr[1] + 1):
        gx = x
        gy = y
        xdir = root + '/' + str(x)
        if os.path.exists(xdir) == False:
            os.makedirs(xdir)
        if options.inverty == 'false':
            gx, gy = gm.GoogleTile(x, y, z)
        url = options.template.format(z=options.zoom, x=gx, y=gy)
        #y2 = y - 303
        fname = xdir + '/' + str(y) + extension
        xscale, xshift, yshift, yscale, xorigin, yorigin = gm.WorldFileParameters(
            x, y, z)
        try:
            image = urllib.URLopener()
            image.retrieve(url, fname)
            newline = str(xscale) + '\n' + str(xshift) + '\n' + str(
                yshift) + '\n' + str(yscale) + '\n' + str(
                    xorigin) + '\n' + str(yorigin)
            file = open(xdir + '/' + str(y) + wf, 'w')
            file.write(newline)
            file.close()
        except:
コード例 #7
0
    tminx, tminy = mercator.MetersToTile(mx, my, tz)

    if boundingbox:
        mx, my = mercator.LatLonToMeters(latmax, lonmax)
        print "Spherical Mercator (ESPG:900913) cooridnate for maxlat/maxlon: "
        print(mx, my)
        tmaxx, tmaxy = mercator.MetersToTile(mx, my, tz)
    else:
        tmaxx, tmaxy = tminx, tminy

    for ty in range(tminy, tmaxy + 1):
        for tx in range(tminx, tmaxx + 1):
            tilefilename = "%s/%s/%s" % (tz, tx, ty)
            print tilefilename, "( TileMapService: z / x / y )"

            gx, gy = mercator.GoogleTile(tx, ty, tz)
            print "\tGoogle:", gx, gy
            quadkey = mercator.QuadTree(tx, ty, tz)
            print "\tQuadkey:", quadkey, '(', int(quadkey, 4), ')'
            bounds = mercator.TileBounds(tx, ty, tz)
            print
            print "\tEPSG:900913 Extent: ", bounds
            wgsbounds = mercator.TileLatLonBounds(tx, ty, tz)
            print "\tWGS84 Extent:", wgsbounds
            print "\tgdalwarp -ts 256 256 -te %s %s %s %s %s %s_%s_%s.tif" % (
                bounds[0], bounds[1], bounds[2], bounds[3],
                "<your-raster-file-in-epsg900913.ext>", tz, tx, ty)
            print

########NEW FILE########
コード例 #8
0
class Downloader(object):
    '''
    Based on http://www.wellho.net/solutions/python-python-threads-a-first-example.html
    '''
    def __init__(self, mapdir, minzoom, maxzoom):
        self.mercator = GlobalMercator(256)
        self.minzoom = minzoom
        self.maxzoom = maxzoom
        self.TopRightLat = None
        self.TopRightLon = None
        self.BottomLeftLat = None
        self.BottomLeftLon = None
        self.mminx = None
        self.mminy = None
        self.mmaxx = None
        self.mmaxy = None
        self.mapdir = mapdir
        self.jobs = Queue.Queue()

    def download(self, toprightlat, toprightlon, bottomleftlat, bottomleftlon):
        self.TopRightLat = toprightlat
        self.TopRightLon = toprightlon
        self.BottomLeftLat = bottomleftlat
        self.BottomLeftLon = bottomleftlon
        self.mminx, self.mminy = self.mercator.LatLonToMeters(
            toprightlat, toprightlon)
        self.mmaxx, self.mmaxy = self.mercator.LatLonToMeters(
            bottomleftlat, bottomleftlon)

        map(self.addJobForZoom, range(self.minzoom, self.maxzoom + 1))

        self.runJobs()

    def addJobForZoom(self, zoom):
        tminx, tminy = self.mercator.MetersToTile(self.mminx, self.mminy, zoom)
        tmaxx, tmaxy = self.mercator.MetersToTile(self.mmaxx, self.mmaxy, zoom)

        if tminx > tmaxx:
            tminx, tmaxx = tmaxx, tminx
        if tminy > tmaxy:
            tminy, tmaxy = tmaxy, tminy

        for tx in range(tminx, tmaxx + 1):
            for ty in range(tminy, tmaxy + 1):
                gx, gy = self.mercator.GoogleTile(tx, ty, zoom)
                self.jobs.put({'x': gx, 'y': gy, 'z': zoom})

    def runJobs(self):
        workers = []
        for threadNum in range(0, MAX_THREADS):
            subdownloader = self.SubDownloader(self)
            workers.append(subdownloader)
            workers[-1].start()

        for worker in workers:
            worker.join(20)

        print "Finished!"

    class SubDownloader(Thread):
        def __init__(self, parent):
            Thread.__init__(self)
            self.parent = parent

        def run(self):
            while 1:
                try:
                    job = self.parent.jobs.get(0)
                except Queue.Empty:
                    return
                mt = random.randrange(0, 4)
                filename = '%i/gm_%i_%i_%i.png' % (job['z'], job['x'],
                                                   job['y'], job['z'])
                if os.path.isfile('%s%s' % (self.parent.mapdir, filename)):
                    #                    print "skippnig", filename, "left:", self.parent.jobs.qsize()
                    continue
                if not os.path.isdir('%s%s' % (self.parent.mapdir, job['z'])):
                    os.mkdir('%s%s' % (self.parent.mapdir, job['z']))
#                http://mt1.google.com/vt/lyrs=m@115&hl=en&x=39141&s=&y=26445&z=16&s=Gali
                url = 'http://mt%i.google.com/vt/lyrs=m@115&hl=en&x=%i&y=%i&z=%i&s=' % (
                    mt, job['x'], job['y'], job['z'])
                try:
                    tile = urllib2.urlopen(url=url, timeout=20).read()
                except:
                    #                    print "Can't open", url, "left:", self.parent.jobs.qsize()
                    continue
                fh = open(filename, 'wb')
                fh.write(tile)
                fh.close()
コード例 #9
0
ファイル: test.py プロジェクト: gboysking/globalmaptiles
print(result)

result = gm.MetersToLatLon(meters['mx'], meters['my'])
print(result)

result = gm.MetersToPixels(meters['mx'], meters['my'], zoom)
print(result)

result = gm.PixelsToTile(pixels['px'], pixels['py'])
print(result)

result = gm.PixelsToMeters(pixels['px'], pixels['py'], zoom)
print(result)

result = gm.TileBounds(tile['tx'], tile['ty'], zoom)
print(result)

result = gm.LatLonToTile(geographic['lat'], geographic['lon'], zoom)
print(result)

result = gm.MetersToTile(meters['mx'], meters['my'], zoom)
print(result)

result = gm.GoogleTile(tile['tx'], tile['ty'], zoom)
print(result)

result = gm.QuadTree(tile['tx'], tile['ty'], zoom)
print(result)

tx, ty, zoom = gm.QuadKeyToTile(quadKey)
print(tx, ty, zoom)
コード例 #10
0
class TileGrid(object):
    #def __init__(self, parentgeometry, bearing = 0.0, zoomlevel = 16, lat = dec.Decimal('32.829608'), lon = dec.Decimal('35.080498')):
    #def __init__(self, parentgeometry, bearing = 0.0, zoomlevel = 16, lat = dec.Decimal('32.330347'), lon = dec.Decimal('34.851395')):
    def __init__(self,
                 bearing=0.0,
                 zoomlevel=16,
                 lat=decimal.Decimal('32.018300'),
                 lon=decimal.Decimal('34.898161'),
                 parent=None):
        #set initial values
        self.parent = parent
        self.bearingSensitivity = decimal.Decimal('0.00001')
        self.bearing = bearing
        self.zoomlevel = zoomlevel
        self.lat = lat
        self.lon = lon
        self.gx, self.gy = None, None
        self.velocity = 0.0

        self.sysPath = os.path.join(sys.path[0], "")

        self.mapPath = self.sysPath
        self.maxZoomLevel = 16

        self.destlat = decimal.Decimal('32.776250')
        self.destlon = decimal.Decimal('35.028946')

        self.distance = 0

        self.setBounds(parent.geometry().width(), parent.geometry().height())

        self.halfboundx = math.ceil(self.boundx / 2)
        self.halfboundy = math.ceil(self.boundy / 2)

        #make GlobalMercator instance
        self.mercator = GlobalMercator()
        #        create pathways
        self.refresh()

    def setMapPath(self, path):
        if path != "":
            self.mapPath = path + "/"
        print self.mapPath

    def setBounds(self, newx, newy):
        self.boundx, self.boundy = newx, newy
        #        adding 16px to halfbounds height and width to display icons
        self.halfboundx = int(math.ceil(self.boundx / 2)) + 31
        self.halfboundy = int(math.ceil(self.boundy / 2)) + 31

    def getOffset(self):
        'get pixel offset of coordinates from 0,0 of tile'
        offpx = self.px - self.tx * self.mercator.tileSize
        #the y pixel coordinate system begins from top
        offpy = self.mercator.tileSize - (self.py -
                                          self.ty * self.mercator.tileSize)
        return offpx, offpy

    def moveTo(self, lat, lon, calculateBearing=True):
        'move position to lat, lon and update all properties'
        #update bearing from previous position
        if calculateBearing:
            if abs(lon - self.lon) > self.bearingSensitivity \
            or abs(lat - self.lat) > self.bearingSensitivity:
                self.bearing = math.degrees(
                    math.atan2(lon - self.lon, lat - self.lat))
                if self.bearing < 0:
                    self.bearing += 360

        self.lat = lat
        self.lon = lon

        #get meters from lat/lon
        mx, my = self.mercator.LatLonToMeters(float(self.lat), float(self.lon))

        #dx, dy = self.mercator.LatLonToMeters( float(self.destlat), float(self.destlon) )

        #self.distance = math.sqrt(math.pow(mx-dx, 2) + math.pow(my-dy, 2 ))

        #get pixels from meters
        self.px, self.py = self.mercator.MetersToPixels(mx, my, self.zoomlevel)

        #get tile from pixels
        self.tx, self.ty = self.mercator.PixelsToTile(int(self.px),
                                                      int(self.py))

        #get google tile
        self.gx, self.gy = self.mercator.GoogleTile(self.tx, self.ty,
                                                    self.zoomlevel)

        #update offset of tile
        self.offpx, self.offpy = self.getOffset()

        #TODO:  calculate loadRect bounds for peripherial tiles

        self.sizex = int(math.ceil(self.boundx / self.mercator.tileSize)) + 1
        self.sizey = int(math.ceil(self.boundy / self.mercator.tileSize)) + 1

        halfdeltax = int(math.ceil(self.sizex / 2)) + 1
        halfdeltay = int(math.ceil(self.sizey / 2)) + 1

        self.images = set()

        offtilex = halfdeltax * self.mercator.tileSize + self.offpx
        for x in range(self.gx - halfdeltax, self.gx + halfdeltax + 1):
            offtiley = halfdeltay * self.mercator.tileSize + self.offpy
            for y in range(self.gy - halfdeltay, self.gy + halfdeltay + 1):
                fname = "%i/gm_%i_%i_%i.png" % (self.zoomlevel, x, y,
                                                self.zoomlevel)
                if not QtCore.QFile.exists(self.mapPath + fname):
                    fname = "404.png"
                self.images.add((QtCore.QPointF(-offtilex, -offtiley), fname))
                offtiley -= self.mercator.tileSize
            offtilex -= self.mercator.tileSize
#            print fname

        self.bounds = {
            'TL': (self.px - self.halfboundx, self.py - self.halfboundy),
            'TR': (self.px + self.halfboundx, self.py - self.halfboundy),
            'BR': (self.px + self.halfboundx, self.py + self.halfboundy),
            'BL': (self.px - self.halfboundx, self.py + self.halfboundy)
        }

        self.pathways = QtGui.QPolygonF()
        #        create waypoints icons
        self.visible_waypoints = set()
        for wp in self.waypoints_pixels:
            x = wp[0] - self.px
            y = self.py - wp[1]
            self.pathways.append(QtCore.QPointF(x, y))
            if self.isVisible(wp):
                self.visible_waypoints.add(QtCore.QPointF(x - 15, y - 32))

#        print self.waypoints_pixels

    def isVisible(self, wp):
        return  wp[0] > self.bounds['TL'][0] and \
                wp[0] < self.bounds['TR'][0] and \
                wp[1] > self.bounds['TL'][1] and \
                wp[1] < self.bounds['BL'][1]

    def refresh(self):
        self.waypoints_pixels = set()
        for wp in self.parent.waypoint:
            wpm = self.mercator.LatLonToMeters(wp[0], wp[1])
            wppx = self.mercator.MetersToPixels(wpm[0], wpm[1], self.zoomlevel)
            self.waypoints_pixels.add(wppx)
        self.moveTo(self.lat, self.lon)

    def setZoom(self, zoom):
        self.zoomlevel = zoom
        self.refresh()

    def zoomIn(self):
        if self.zoomlevel < self.maxZoomLevel:
            self.zoomlevel += 1
            self.refresh()

    def zoomOut(self):
        if self.zoomlevel > 0:
            self.zoomlevel -= 1
            self.refresh()

    def setBearing(self, bear):
        self.bearing = bear

    def setVelocity(self, vel):
        self.velocity = vel