void OpenstreetmapMapProvider::getTiles(const QGeoCoordinate& topleft,
    int zoomLevel,
    int width,
    int height)
{
    cancelDownload();

    double       tilex_exact = long2tilex(topleft.longitude(), zoomLevel);
    double       tiley_exact = lat2tiley(topleft.latitude(), zoomLevel);

    int          x_start = (int)floor(tilex_exact);
    int          y_start = (int)floor(tiley_exact);

    int          x_end = (int)floor(tilex_exact) + width / TILE_DIMENSION + 1;
    int          y_end = (int)floor(tiley_exact) + height / TILE_DIMENSION + 1;

    QQueue<Tile> list;

    for (int y = y_start; y <= y_end; y++)
    {
        for (int x = x_start; x <= x_end; x++)
        {
            Tile info;
            info.x    = x * TILE_DIMENSION;
            info.y    = y * TILE_DIMENSION;
            info.w    = TILE_DIMENSION;
            info.h    = TILE_DIMENSION;
            info.zoom = zoomLevel;

            list.enqueue(info);
        }
    }
    startDownload(list);
}
Ejemplo n.º 2
0
/**
 * Initializes window, opengl, and camera at the given coordinates
 */
void FlyerWindow::init(float const& lat, float const& lon)
{
    App.Create(sf::VideoMode(800,600,32),"OpenStreetMap Flyer");
    App.SetFramerateLimit(60);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glClearDepth(1.0f);
    glClearColor(0.7f,0.8f,1.0f,0.0f);
    glDepthMask(GL_TRUE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.0f,1.0f,1.0f,1000.0f);

    float startingTileX = long2tilex(lon,zoom);
    float startingTileY = lat2tiley(lat,zoom);
    cam.setPosition(tile2pos(startingTileX),100.0,tile2pos(startingTileY));
    cam.look();
}
void OpenstreetmapMapProvider::getTile(const QGeoCoordinate& coord,
    int zoomLevel)
{
    cancelDownload();

    double tilex_exact = long2tilex(coord.longitude(), zoomLevel);
    double tiley_exact = lat2tiley(coord.latitude(), zoomLevel);

    Tile   info;
    info.x    = tilex_exact;
    info.y    = tiley_exact;
    info.w    = TILE_DIMENSION;
    info.h    = TILE_DIMENSION;
    info.zoom = zoomLevel;

    QQueue<Tile> queue;
    queue.enqueue(info);

    startDownload(queue);
}
Ejemplo n.º 4
0
int main(int argc, char* argv[])
{
  std::string   map;
  std::string   style;
  double        latTop,latBottom,lonLeft,lonRight;
  unsigned long xTileStart,xTileEnd,xTileCount,yTileStart,yTileEnd,yTileCount;
  unsigned long startZoom;
  unsigned long endZoom;
  unsigned long tileWidth;
  unsigned long tileHeight;
  std::string   driver;

  if (argc!=12) {
    std::cerr << "DrawMap ";
    std::cerr << "<map directory> <style-file> ";
    std::cerr << "<lat_top> <lon_left> <lat_bottom> <lon_right> ";
    std::cerr << "<start zoom>" << std::endl;
    std::cerr << "<end zoom>" << std::endl;
    std::cerr << "<tile width>" << std::endl;
    std::cerr << "<tile height>" << std::endl;
    std::cerr << "<driver>" << std::endl;
    return 1;
  }

  map=argv[1];
  style=argv[2];

  if (sscanf(argv[3],"%lf",&latTop)!=1) {
    std::cerr << "lon is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[4],"%lf",&lonLeft)!=1) {
    std::cerr << "lat is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[5],"%lf",&latBottom)!=1) {
    std::cerr << "lon is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[6],"%lf",&lonRight)!=1) {
    std::cerr << "lat is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[7],"%lu",&startZoom)!=1) {
    std::cerr << "start zoom is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[8],"%lu",&endZoom)!=1) {
    std::cerr << "end zoom is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[9],"%lu",&tileWidth)!=1) {
    std::cerr << "tile width is not numeric!" << std::endl;
    return 1;
  }

  if (sscanf(argv[10],"%lu",&tileHeight)!=1) {
    std::cerr << "tile height is not numeric!" << std::endl;
    return 1;
  }

  driver=argv[11];

#if defined(HAVE_LIB_OSMSCOUTMAPCAIRO)
  cairo_surface_t *surface=NULL;
  cairo_t         *cairo=NULL;
#endif

  if (driver=="cairo") {
    std::cout << "Using driver 'cairo'..." << std::endl;
#if defined(HAVE_LIB_OSMSCOUTMAPCAIRO)
    surface=cairo_image_surface_create(CAIRO_FORMAT_RGB24,tileWidth,tileHeight);

    if (surface==NULL) {
      std::cerr << "Cannot create cairo image surface" << std::endl;
      return 1;
    }

    cairo=cairo_create(surface);

    if (cairo==NULL) {
      std::cerr << "Cannot create cairo_t for image surface" << std::endl;
      return 1;
    }
#else
    std::cerr << "Driver 'cairo' is not enabled" << std::endl;
    return 1;
#endif
  }
  else {
    std::cerr << "Unsupported driver '" << driver << "'" << std::endl;
    return 1;
  }

  osmscout::DatabaseParameter databaseParameter;

  //databaseParameter.SetDebugPerformance(true);

  osmscout::Database          database(databaseParameter);

  if (!database.Open(map.c_str())) {
    std::cerr << "Cannot open database" << std::endl;
    return 1;
  }

  osmscout::StyleConfig styleConfig(database.GetTypeConfig());

  if (!osmscout::LoadStyleConfig(style.c_str(),styleConfig)) {
    std::cerr << "Cannot open style" << std::endl;
    return 1;
  }

  osmscout::MercatorProjection  projection;
  osmscout::MapParameter        drawParameter;
  osmscout::AreaSearchParameter searchParameter;

  for (size_t zoom=std::min(startZoom,endZoom);
       zoom<=std::max(startZoom,endZoom);
       zoom++) {
    xTileStart=long2tilex(std::min(lonLeft,lonRight),zoom);
    xTileEnd=long2tilex(std::max(lonLeft,lonRight),zoom);
    xTileCount=xTileEnd-xTileStart+1;

    yTileStart=lat2tiley(std::max(latTop,latBottom),zoom);
    yTileEnd=lat2tiley(std::min(latTop,latBottom),zoom);
    yTileCount=yTileEnd-yTileStart+1;

    std::cout << "Drawing zoom " << zoom;
    //<< ", " << (xTileCount)*(yTileCount) << " tiles [" << xTileStart << "," << yTileStart << " - " <<  xTileEnd << "," << yTileEnd << "]";
    std::cout << std::endl;

#if defined(HAVE_LIB_OSMSCOUTMAPCAIRO)
    osmscout::MapPainterCairo cairoPainter;
#endif

    osmscout::Magnification   magnification;

    magnification.SetLevel(zoom);

    double dbMinTime=std::numeric_limits<double>::max();
    double dbMaxTime=0.0;
    double dbTotalTime=0.0;

    double drawMinTime=std::numeric_limits<double>::max();
    double drawMaxTime=0.0;
    double drawTotalTime=0.0;

    for (size_t y=yTileStart; y<=yTileEnd; y++) {
      for (size_t x=xTileStart; x<=xTileEnd; x++) {
        double                         lat,lon;
        osmscout::TypeSet              nodeTypes;
        std::vector<osmscout::TypeSet> wayTypes;
        osmscout::TypeSet              areaTypes;
        osmscout::MapData              data;

        lat=(tiley2lat(y,zoom)+tiley2lat(y+1,zoom))/2;
        lon=(tilex2long(x,zoom)+tilex2long(x+1,zoom))/2;

        //std::cout << "Drawing tile at " << lat << "," << lon << "/";
        //std::cout << x << "," << y << "/";
        //std::cout << x-xTileStart << "," << y-yTileStart << std::endl;

        projection.Set(lon,
                       lat,
                       magnification,
                       tileWidth,
                       tileHeight);

        styleConfig.GetNodeTypesWithMaxMag(projection.GetMagnification(),
                                           nodeTypes);

        styleConfig.GetWayTypesByPrioWithMaxMag(projection.GetMagnification(),
                                                wayTypes);

        styleConfig.GetAreaTypesWithMaxMag(projection.GetMagnification(),
                                           areaTypes);

        osmscout::StopClock dbTimer;

        database.GetObjects(nodeTypes,
                            wayTypes,
                            areaTypes,
                            projection.GetLonMin(),
                            projection.GetLatMin(),
                            projection.GetLonMax(),
                            projection.GetLatMax(),
                            projection.GetMagnification(),
                            searchParameter,
                            data.nodes,
                            data.ways,
                            data.areas);

        dbTimer.Stop();

        double dbTime=dbTimer.GetMilliseconds();

        dbMinTime=std::min(dbMinTime,dbTime);
        dbMaxTime=std::max(dbMaxTime,dbTime);
        dbTotalTime+=dbTime;

        osmscout::StopClock drawTimer;

#if defined(HAVE_LIB_OSMSCOUTMAPCAIRO)
        if (driver=="cairo") {
          //std::cout << data.nodes.size() << " " << data.ways.size() << " " << data.areas.size() << std::endl;
          cairoPainter.DrawMap(styleConfig,
                               projection,
                               drawParameter,
                               data,
                               cairo);
        }
#endif

        drawTimer.Stop();

        double drawTime=drawTimer.GetMilliseconds();

        drawMinTime=std::min(drawMinTime,drawTime);
        drawMaxTime=std::max(drawMaxTime,drawTime);
        drawTotalTime+=drawTime;
      }
    }

    std::cout << "GetObjects: ";
    std::cout << "total: " << dbTotalTime << " msec ";
    std::cout << "min: " << dbMinTime << " msec ";
    std::cout << "avg: " << dbTotalTime/(xTileCount*yTileCount) << " msec ";
    std::cout << "max: " << dbMaxTime << " msec" << std::endl;

    std::cout << "DrawMap: ";
    std::cout << "total: " << drawTotalTime << " msec ";
    std::cout << "min: " << drawMinTime << " msec ";
    std::cout << "avg: " << drawTotalTime/(xTileCount*yTileCount) << " msec ";
    std::cout << "max: " << drawMaxTime << " msec" << std::endl;
  }

  database.Close();

#if defined(HAVE_LIB_OSMSCOUTMAPCAIRO)
  if (driver=="cairo") {
    cairo_destroy(cairo);
    cairo_surface_destroy(surface);
  }
#endif
  return 0;
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
    char *spath = strdup(RENDER_SOCKET);
    const char *mapname_default = XMLCONFIG_DEFAULT;
    const char *mapname = mapname_default;
    const char *tile_dir = tile_dir_default;
    int minX=-1, maxX=-1, minY=-1, maxY=-1;
    int x, y, z;
    char name[PATH_MAX];
    struct timeval start, end;
    int num_render = 0, num_all = 0;
    int c;
    int all=0;
    int numThreads = 1;
    int force=0;
    int bboxset=0;
    struct storage_backend * store;
    struct stat_info s;

    while (1) {
        int option_index = 0;
        static struct option long_options[] = {
            {"min-zoom", 1, 0, 'z'},
            {"max-zoom", 1, 0, 'Z'},
            {"min-x", 1, 0, 'x'},
            {"max-x", 1, 0, 'X'},
            {"min-y", 1, 0, 'y'},
            {"max-y", 1, 0, 'Y'},
            {"socket", 1, 0, 's'},
            {"num-threads", 1, 0, 'n'},
            {"max-load", 1, 0, 'l'},
            {"tile-dir", 1, 0, 't'},
            {"map", 1, 0, 'm'},
            {"verbose", 0, 0, 'v'},
            {"force", 0, 0, 'f'},
            {"all", 0, 0, 'a'},
            {"bbox", 1, 0, 'b'},
            {"help", 0, 0, 'h'},
            {0, 0, 0, 0}
        };

        //c = getopt_long(argc, argv, "hvaz:Z:x:X:y:Y:s:m:t:n:l:f", long_options, &option_index);
        c = getopt_long(argc, argv, "hvab:z:Z:x:X:y:Y:s:m:t:n:l:f", long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
            case 'a':   /* -a, --all */
                all=1;
                break;
            case 'b':   /* -b, --bbox */
                if (!handle_bbox(optarg))
                {
                    fprintf(stderr, "invalid bbox argument - must be of the form east,south,west,north\n");
                    return -1;
                }
                bboxset = 1;
                break;
            case 's':   /* -s, --socket */
                free(spath);
                spath = strdup(optarg);
                break;
            case 't':   /* -t, --tile-dir */
                tile_dir=strdup(optarg);
                break;
            case 'm':   /* -m, --map */
                mapname=strdup(optarg);
                break;
            case 'l':   /* -l, --max-load */
                maxLoad = atoi(optarg);
                break;
            case 'n':   /* -n, --num-threads */
                numThreads=atoi(optarg);
                if (numThreads <= 0) {
                    fprintf(stderr, "Invalid number of threads, must be at least 1\n");
                    return 1;
                }
                break;
            case 'x':   /* -x, --min-x */
                minX=atoi(optarg);
                break;
            case 'X':   /* -X, --max-x */
                maxX=atoi(optarg);
                break;
            case 'y':   /* -y, --min-y */
                minY=atoi(optarg);
                break;
            case 'Y':   /* -Y, --max-y */
                maxY=atoi(optarg);
                break;
            case 'z':   /* -z, --min-zoom */
                minZoom=atoi(optarg);
                if (minZoom < 0 || minZoom > MAX_ZOOM) {
                    fprintf(stderr, "Invalid minimum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
                    return 1;
                }
                break;
            case 'Z':   /* -Z, --max-zoom */
                maxZoom=atoi(optarg);
                if (maxZoom < 0 || maxZoom > MAX_ZOOM) {
                    fprintf(stderr, "Invalid maximum zoom selected, must be between 0 and %d\n", MAX_ZOOM);
                    return 1;
                }
                break;
            case 'f':   /* -f, --force */
                force=1;
                break;
            case 'v':   /* -v, --verbose */
                verbose=1;
                break;
            case 'h':   /* -h, --help */
                fprintf(stderr, "Usage: render_list [OPTION] ...\n");
                fprintf(stderr, "  -a, --all            render all tiles in given zoom level range instead of reading from STDIN\n");
                fprintf(stderr, "  -b, --bbox           render all tiles in given bbox (min Longitude, min Latitude, max Longitude, max Latitude) range instead of reading from STDIN\n");
                fprintf(stderr, "  -f, --force          render tiles even if they seem current\n");
                fprintf(stderr, "  -m, --map=MAP        render tiles in this map (defaults to '" XMLCONFIG_DEFAULT "')\n");
                fprintf(stderr, "  -l, --max-load=LOAD  sleep if load is this high (defaults to %d)\n", MAX_LOAD_OLD);
                fprintf(stderr, "  -s, --socket=SOCKET  unix domain socket name for contacting renderd\n");
                fprintf(stderr, "  -n, --num-threads=N the number of parallel request threads (default 1)\n");
                fprintf(stderr, "  -t, --tile-dir       tile cache directory (defaults to '" HASH_PATH "')\n");
                fprintf(stderr, "  -z, --min-zoom=ZOOM  filter input to only render tiles greater or equal to this zoom level (default is 0)\n");
                fprintf(stderr, "  -Z, --max-zoom=ZOOM  filter input to only render tiles less than or equal to this zoom level (default is %d)\n", MAX_ZOOM);
                fprintf(stderr, "If you are using --all, you can restrict the tile range by adding these options:\n");
                fprintf(stderr, "  -x, --min-x=X        minimum X tile coordinate\n");
                fprintf(stderr, "  -X, --max-x=X        maximum X tile coordinate\n");
                fprintf(stderr, "  -y, --min-y=Y        minimum Y tile coordinate\n");
                fprintf(stderr, "  -Y, --max-y=Y        maximum Y tile coordinate\n");
                fprintf(stderr, "Without --all, send a list of tiles to be rendered from STDIN in the format:\n");
                fprintf(stderr, "  X Y Z\n");
                fprintf(stderr, "e.g.\n");
                fprintf(stderr, "  0 0 1\n");
                fprintf(stderr, "  0 1 1\n");
                fprintf(stderr, "  1 0 1\n");
                fprintf(stderr, "  1 1 1\n");
                fprintf(stderr, "The above would cause all 4 tiles at zoom 1 to be rendered\n");
                return -1;
            default:
                fprintf(stderr, "unhandled char '%c'\n", c);
                break;
        }
    }

    if (maxZoom < minZoom) {
        fprintf(stderr, "Invalid zoom range, max zoom must be greater or equal to minimum zoom\n");
        return 1;
    }

    store = init_storage_backend(tile_dir);
    if (store == NULL) {
        fprintf(stderr, "Failed to initialise storage backend %s\n", tile_dir);
        return 1;
    }

    if (all) {
        if ((minX != -1 || minY != -1 || maxX != -1 || maxY != -1) && minZoom != maxZoom) {
            fprintf(stderr, "min-zoom must be equal to max-zoom when using min-x, max-x, min-y, or max-y options\n");
            return 1;
        }

        if (minX == -1) { minX = 0; }
        if (minY == -1) { minY = 0; }

        int lz = (1 << minZoom) - 1;

        if (minZoom == maxZoom) {
            if (maxX == -1) { maxX = lz; }
            if (maxY == -1) { maxY = lz; }
            if (minX > lz || minY > lz || maxX > lz || maxY > lz) {
                fprintf(stderr, "Invalid range, x and y values must be <= %d (2^zoom-1)\n", lz);
                return 1;
            }
        }

        if (minX < 0 || minY < 0 || maxX < -1 || maxY < -1) {
            fprintf(stderr, "Invalid range, x and y values must be >= 0\n");
            return 1;
        }

    }

    fprintf(stderr, "Rendering client\n");

    gettimeofday(&start, NULL);

    spawn_workers(numThreads, spath, maxLoad);

    if (all) {
        int x, y, z;
        printf("Rendering all tiles from zoom %d to zoom %d\n", minZoom, maxZoom);
        for (z=minZoom; z <= maxZoom; z++) {
            int current_maxX = (maxX == -1) ? (1 << z)-1 : maxX;
            int current_maxY = (maxY == -1) ? (1 << z)-1 : maxY;
            printf("Rendering all tiles for zoom %d from (%d, %d) to (%d, %d)\n", z, minX, minY, current_maxX, current_maxY);
            for (x=minX; x <= current_maxX; x+=METATILE) {
                for (y=minY; y <= current_maxY; y+=METATILE) {
                    if (!force) s = store->tile_stat(store, mapname, "", x, y, z);
                    if (force || (s.size < 0) || (s.expired)) {
                        enqueue(mapname, x, y, z);
                        num_render++;
                    }
                    num_all++;

                }
            }
        }
      } else if (bboxset) {
        int x, y, z, mask = METATILE - 1;
        printf("Rendering bbox (%.6f, %.6f) to (%.6f, %.6f) tiles from zoom %d to zoom %d\n", bbox[0], bbox[1], bbox[2], bbox[3], minZoom, maxZoom);
        for (z=minZoom; z <= maxZoom; z++) {
            minX = long2tilex(bbox[0], z) & ~mask;
            minY = lat2tiley(bbox[1], z) & ~mask;
            maxX = long2tilex(bbox[2], z) & ~mask;
            maxY = lat2tiley(bbox[3], z) & ~mask;
            if (minX < 0 || minY < 0 || maxX < -1 || maxY < -1) {
                fprintf(stderr, "Invalid range, x and y values must be >= 0\n");
                fprintf(stderr, "Zoom %d from (%d, %d) to (%d, %d)\n", z, minX, minY, maxX, maxY);
                fprintf(stderr, "Exit from zoom %d\n", z);
                continue;
            }
            if (minX > maxX) {
                // swap minX and maxX
                minX = minX ^ maxX;
                maxX = minX ^ maxX;
                minX = minX ^ maxX;
            }
            if (minY > maxY) {
                // swap minY and maxY
                minY = minY ^ maxY;
                maxY = minY ^ maxY;
                minY = minY ^ maxY;
            }
            printf("Rendering bbox tiles for zoom %d from (%d, %d) to (%d, %d)\n", z, minX, minY, maxX, maxY);
            for (x=minX; x <= maxX; x+=METATILE) {
                for (y=minY; y <= maxY; y+=METATILE) {
                    if (!force) s = store->tile_stat(store, mapname, "", x, y, z);
                    if (force || (s.size < 0) || (s.expired)) {
                        enqueue(mapname, x, y, z);
                        num_render++;
                    }
                    num_all++;
                }
            }
         }
    } else {
        while(!feof(stdin)) {
            int n = fscanf(stdin, "%d %d %d", &x, &y, &z);

            if (verbose)


            if (n != 3) {
                // Discard input line
                char tmp[1024];
                char *r = fgets(tmp, sizeof(tmp), stdin);
                if (!r)
                    continue;
                fprintf(stderr, "bad line %d: %s", num_all, tmp);
                continue;
            }

            if (verbose)
                printf("got: x(%d) y(%d) z(%d)\n", x, y, z);

            if (z < minZoom || z > maxZoom) {
                printf("Ignoring tile, zoom %d outside valid range (%d..%d)\n", z, minZoom, maxZoom);
                continue;
            }

            num_all++;

            if (!force) s = store->tile_stat(store, mapname, "", x, y, z);
            if (force || (s.size < 0) || (s.expired)) {
                // missing or old, render it
                //ret = process_loop(fd, mapname, x, y, z);
                enqueue(mapname, x, y, z);
                num_render++;
                // Attempts to adjust the stats for the QMAX tiles which are likely in the queue
                if (!(num_render % 10)) {
                    gettimeofday(&end, NULL);
                    printf("\n");
                    printf("Meta tiles rendered: ");
                    display_rate(start, end, num_render);
                    printf("Total tiles rendered: ");
                    display_rate(start, end, (num_render) * METATILE * METATILE);
                    printf("Total tiles handled from input: ");
                    display_rate(start, end, num_all);
                }
            } else {
                if (verbose)
                    printf("Tile %s is clean, ignoring\n", store->tile_storage_id(store, mapname, "", x, y, z, name));
            }
        }
    }

    store->close_storage(store);
    if (store != NULL) {
        free(store);
    }
    finish_workers();

    free(spath);
    if (mapname != mapname_default) {
        free((void *)mapname);
    }
    if (tile_dir != tile_dir_default) {
        free((void *)tile_dir);
    }


    gettimeofday(&end, NULL);
    printf("\n*****************************************************\n");
    printf("*****************************************************\n");
    printf("Total for all tiles rendered\n");
    printf("Meta tiles rendered: ");
    display_rate(start, end, num_render);
    printf("Total tiles rendered: ");
    display_rate(start, end, num_render * METATILE * METATILE);
    printf("Total tiles handled: ");
    display_rate(start, end, num_all);
    print_statistics();

    return 0;
}
QPoint OpenstreetmapMapProvider::coordToPixel(const QGeoCoordinate& coord,
    int zoomLevel) const
{
    return QPoint(long2tilex(coord.longitude(), zoomLevel) * TILE_DIMENSION,
               lat2tiley(coord.latitude(), zoomLevel) * TILE_DIMENSION);
}