【问题标题】:Use tileserver-gl host in qt map在 qt 地图中使用 tileserver-gl 主机
【发布时间】:2020-10-16 14:29:34
【问题描述】:

我已经设置了一个本地图块服务器以与我的应用程序一起使用,但是当我创建 QML 地图对象并指定插件以使用自定义主机时,应用程序不使用本地图块。循环浏览supportedMapTypes 并在地图对象上设置activeMapType 属性将显示一些图块,但它们似乎是一组默认图块,而不是来自我本地图块服务器的图块。

Map
{
    id: map
    objectName: "basemap"
    anchors.fill: parent
    plugin: Plugin
    {
        name: "osm"
        PluginParameter
        {
            name: "osm.mapping.custom.host"
            value: "http://localhost:8080/data/openmaptiles_satellite_lowres/"
        }
    }
    zoomLevel: 1
    activeMapType: // varies depending on selection from another object in application
    center: QtPositioning.coordinate(0, 0)
}

我知道磁贴服务器运行正常,因为我可以通过导航到localhost:8080 在浏览器中访问它,并且我可以使用http://localhost:8080/data/openmaptiles_satellite_lowres/{z}/{y}/{x}.jpg 访问任意磁贴

更新

我正在尝试覆盖 TomasL 下面建议的默认提供程序存储库文件,但应用程序似乎没有使用指定的插件参数。

Mapper.qml 中的地图组件

Map {
  id: basemap
  objectName: "basemap"
  anchors.fill: parent
  plugin: ProvidersPlugin {}

  activeMapType: supportedMapTypes[1] // To use the satellite file in providers repository
  center: QtPositioning.coordinate(0, 0)
  zoomLevel: 2
  minimumZoomLevel: 0
  maximumZoomLevel: 5
}

ProvidersPlugin.qml

import QtLocation 5.5
import QtPositioning 5.5

Plugin {
  id: mapPlugin

  name: "osm"

  PluginParameter {
    name: "osm.mapping.providersrepository.address"
    value: Qt.resolvedUrl('./providers')
  }
}

./providers/satellite

{
  "Enabled" : true,
  "UrlTemplate" : "http://localhost:8080/data/openmaptiles_satellite_lowres/%z/%x/%y.jpg",
  "ImageFormat" : "jpg",
  "QImageFormat" : "Indexed8",
  "MapCopyRight" : "Test",
  "DataCopyRight" : "Hello World",
  "MinimumZoomLevel" : 0,
  "MaximumZoomLevel" : 5,
}

使用上面的代码,我的应用程序仍然尝试访问默认服务器otile1.mqcdn.com

【问题讨论】:

    标签: qt qml maps tileserver-gl


    【解决方案1】:

    问题是您使用的媒体提供 jpg 图像,但 Qt OSM 插件仅支持 png 格式。一种解决方案是克隆Qt Location模块,修改源代码以便设置、编译和安装图像格式。

    为了简化该任务,我为 Qt 5.15.1 创建了一个补丁:

    tile_image_format.patch

    diff --git a/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp b/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
    index 22c32342..d4747a0a 100644
    --- a/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
    +++ b/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
    @@ -217,11 +217,16 @@ QGeoTiledMappingManagerEngineOsm::QGeoTiledMappingManagerEngineOsm(const QVarian
             if (parameters.contains(QStringLiteral("osm.mapping.copyright")))
                 m_customCopyright = parameters.value(QStringLiteral("osm.mapping.copyright")).toString();
     
    +        QString format = "png";
    +        if(parameters.contains(QStringLiteral("osm.mapping.custom.format"))){
    +            format = parameters.value(QStringLiteral("osm.mapping.custom.format")).toString();
    +        }
    +
             m_providers.push_back(
                 new QGeoTileProviderOsm( nmCached,
                     QGeoMapType(QGeoMapType::CustomMap, tr("Custom URL Map"), tr("Custom url map view set via urlprefix parameter"), false, false, 8, pluginName, cameraCaps),
    -                { new TileProvider(tmsServer + QStringLiteral("%z/%x/%y.png"),
    -                    QStringLiteral("png"),
    +                { new TileProvider(tmsServer + QStringLiteral("%z/%x/%y.") + format,
    +                    format,
                         mapCopyright,
                         dataCopyright) }, cameraCaps
                     ));
    

    上述步骤可以概括为:

    git clone -b 5.15.1 https://github.com/qt/qtlocation.git
    cd qtlocation/src/plugins/geoservices/osm
    wget https://raw.githubusercontent.com/eyllanesc/stackoverflow/master/questions/64391146/tile_image_format.patch
    git apply tile_image_format.patch
    qmake
    make
    make install
    

    另一方面,您必须在 MapType.CustomMap 中指向 activeMapType:

    Map
    {
        id: map
        anchors.fill: parent
        plugin: Plugin
        {
            name: "osm"
            PluginParameter
            {
                name: "osm.mapping.custom.host"
                value: "http://localhost:8080/data/openmaptiles_satellite_lowres/"
            }
            PluginParameter
            {
                name: "osm.mapping.custom.format"
                value: "jpg"
            }
        }
        zoomLevel: 1
        center: QtPositioning.coordinate(0, 0)
        activeMapType: MapType.CustomMap
    }
    

    【讨论】:

    • 这是最终对我有用的解决方案,只需稍作修改。我正在使用(并且仅限于使用)的 QT 版本低于 5.15,所以我不得不进入源代码并手动应用补丁。
    【解决方案2】:

    一种不需要 Qt 补丁的简单方法是按原样使用 OpenStreetMap 插件,但为插件设置另一个提供程序存储库地址。

    import QtLocation 5.12
    
    Plugin {
        id: pluginN
    
        property string projectname: ""
        name: "osm"
        PluginParameter {
                 name: "osm.mapping.providersrepository.address"
                 value: "qrc:/Common/Engine/Source/Plugins/N/NRedirect/"  //or wherever you place your redirect files
             }
    
        PluginParameter { name: "osm.mapping.copyright"; value: "(c) N /OpenStreetMap" }
    }
    

    然后,您可以将所有名为cycle、hiking、night-transit、satellite、street、terrain、transit 的地图层重定向文件放在qrc:/Common/Engine/Source/Plugins/N/NRedirect 文件夹中。如果你会只使用卫星层​​,然后跳过所有其他文件。卫星文件可能看起来像这样(这才是真正的魔力所在!)

        {
            "UrlTemplate" : "http://localhost:8080/data/openmaptiles_satellite_lowres/%z/%x/%y.jpg",
            "ImageFormat" : "jpg",
            "QImageFormat" : "Indexed8",
            "MapCopyRight" : "OpenStreetMap",
            "DataCopyRight" : "OpenStreetMap",
            "Timestamp" : "2019-02-01"
        }
    

    请注意,在您的示例网址中,http://localhost:8080/data/openmaptiles_satellite_lowres/{z}/{y}/{x}.jpg ,z/x/y 的顺序与大多数示例不同。如果以这种方式设置服务器,则需要更改上面的 UrlTemplate。

    更多细节可以在 osm 插件的源代码中找到: https://github.com/qt/qtlocation/blob/dev/src/plugins/geoservices/osm/qgeotileproviderosm.cpp#L392-L568

    【讨论】:

    • 我正在尝试使用这种方法,但地图对象似乎完全忽略了插件。我在应用程序中创建了一个名为providers 的目录,其中包含一个satellite 文件,如上所述。根据this blog post,我可以使用file:///。我正在使用value: Qt.resolvedUrl('./providers'),它返回正确的值,但应用程序没有使用本地图块。我也尝试过调整activeMapType,但没有成功。
    • @HotWax:您是否设置了地图组件以使用您的自定义插件? Like plugin: YourPluginName {} 在我的应用程序中,我已将顶部代码放在 YourPluginName.qml 文件中的答案中,并按照地图中的说明使用它。
    • @HotWax 另请注意,如果设置osm.mapping.custom.host 参数,则需要将activeMapType 设置为MapType.CustomMap,如stackoverflow.com/questions/41790875/… 的答案之一所述
    • 我已尝试按照建议将插件放入另一个文件并在我的地图组件中使用它,但尽管这样做,应用程序仍会尝试联系otile1.mqcdn.com,而不是列出的磁贴服务器在satellite 文件中
    • @HotWax 我认为您需要发布更新后的代码才能对此发表评论。例如,您是否记得删除 osm.mapping.custom.host 的插件参数,因为您现在尝试改用 osm.mapping.providersrepository.address
    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多