예제 #1
0
def load_map(projection, center, height, width):
    CACHE_DIR = app.config['CACHE_DIR']
    filename = _get_filename(projection, center, height, width)

    if _maps_cache.get(filename) is None or True:
        try:
            basemap = pickle.load(open(CACHE_DIR + "/" + filename))
        except:
            if projection in ['npstere', 'spstere']:
                basemap = Basemap(
                    resolution='i',
                    ellps='WGS84',
                    projection=projection,
                    boundinglat=center[0],
                    lon_0=center[1])
            elif projection == 'merc':
                basemap = Basemap(
                    resolution='i',
                    ellps='WGS84',
                    projection=projection,
                    llcrnrlat=height[0],
                    llcrnrlon=height[1],
                    urcrnrlat=width[0],
                    urcrnrlon=width[1],
                )
            else:
                basemap = Basemap(
                    resolution='i',
                    ellps='WGS84',
                    projection=projection,
                    lat_0=center[0],
                    lon_0=center[1],
                    height=height,
                    width=width)
            basemap.filename = filename

            def do_pickle(basemap, filename):
                pickle.dump(basemap, open(filename, 'wb'), -1)

            if not os.path.isdir(CACHE_DIR):
                os.makedirs(CACHE_DIR)

            t = threading.Thread(target=do_pickle, args=(basemap, CACHE_DIR +
                                                         "/" + filename))
            t.daemon = True
            t.start()
        _maps_cache[filename] = basemap
    else:
        basemap = _maps_cache[filename]

    return basemap
예제 #2
0
def load_map(projection, center, height, width):
    CACHE_DIR = app.config['CACHE_DIR']
    filename = _get_filename(projection, center, height, width)

    if _maps_cache.get(filename) is None or True:
        try:
            basemap = pickle.load(open(CACHE_DIR + "/" + filename))
        except:
            if projection in ['npstere', 'spstere']:
                basemap = Basemap(resolution='i',
                                  ellps='WGS84',
                                  projection=projection,
                                  boundinglat=center[0],
                                  lon_0=center[1])
            elif projection == 'merc':
                basemap = Basemap(
                    resolution='i',
                    ellps='WGS84',
                    projection=projection,
                    llcrnrlat=height[0],
                    llcrnrlon=height[1],
                    urcrnrlat=width[0],
                    urcrnrlon=width[1],
                )
            else:
                basemap = Basemap(resolution='i',
                                  ellps='WGS84',
                                  projection=projection,
                                  lat_0=center[0],
                                  lon_0=center[1],
                                  height=height,
                                  width=width)
            basemap.filename = filename

            def do_pickle(basemap, filename):
                pickle.dump(basemap, open(filename, 'wb'), -1)

            if not os.path.isdir(CACHE_DIR):
                os.makedirs(CACHE_DIR)

            t = threading.Thread(target=do_pickle,
                                 args=(basemap, CACHE_DIR + "/" + filename))
            t.daemon = True
            t.start()
        _maps_cache[filename] = basemap
    else:
        basemap = _maps_cache[filename]

    return basemap
def load_map(projection: str,
             center: tuple,
             height: float,
             width: float,
             min_lat: float = 0) -> Basemap:

    CACHE_DIR = current_app.config['CACHE_DIR']
    filename = _get_filename(projection, center, height, width)

    def get_resolution(h: float, w: float) -> str:
        area_km = (h * w) / (1000 * 1000)

        if area_km < 10000:
            return 'f'  # full resolution
        elif area_km < 100000:
            return 'h'  # high resolution
        elif area_km < 1000000:
            return 'i'  # intermediate resolution
        elif area_km < 10000000:
            return 'l'  # low resolution
        else:
            return 'c'  # crude resolution

    if _maps_cache.get(filename) is None or True:
        filename = "".join([CACHE_DIR, "/", filename])

        try:
            basemap = pickle.load(open(filename))
        except:
            if projection in ['npstere', 'spstere']:
                basemap = Basemap(
                    resolution=get_resolution(height, width),
                    area_thresh=((height * width) / (1000 * 1000)) *
                    0.00001,  #display islands whose area is 0.001% of displayed area 
                    ellps='WGS84',
                    projection=projection,
                    boundinglat=min_lat,
                    lon_0=center[1],
                )
            elif projection == 'merc':
                basemap = Basemap(
                    resolution='c',
                    #area_thresh=((height*width)/(1000*1000))*0.00001 , #display islands whose area is 0.001% of displayed area
                    ellps='WGS84',
                    projection=projection,
                    llcrnrlat=height[0],
                    llcrnrlon=width[0],
                    urcrnrlat=height[1],
                    urcrnrlon=width[1])
            else:
                basemap = Basemap(
                    resolution=get_resolution(height, width),
                    area_thresh=((height * width) / (1000 * 1000)) *
                    0.00001,  #display islands whose area is 0.001% of displayed area 
                    ellps='WGS84',
                    projection=projection,
                    lat_0=center[0],
                    lon_0=_convert_to_bounded_lon(center[1]),
                    height=height,
                    width=width)
            basemap.filename = filename

            def do_pickle(basemap: Basemap, filename: str) -> None:
                pickle.dump(basemap, open(filename, 'wb'), -1)

            if not os.path.isdir(CACHE_DIR):
                os.makedirs(CACHE_DIR)

            t = threading.Thread(target=do_pickle, args=(basemap, filename))
            t.daemon = True
            t.start()

        _maps_cache[filename] = basemap
    else:
        basemap = _maps_cache[filename]

    return basemap