【问题标题】:How to make heatmap with leaflet and geoJson file data如何使用传单和 geoJson 文件数据制作热图
【发布时间】:2021-04-12 15:01:07
【问题描述】:

我在 Qgis 中有一张热图,并使用 qgis2web 插件生成了预览页面。但是,它在页面上什么也没显示。我做了一些研究,做了一些测试,但我无法使用 geoJson 文件中的数据生成传单热图。有人能帮我吗? 遵循生成的代码。 谢谢。

使用数据遵循文件的模型:

var json_dados_centroids = {
  "type": "FeatureCollection",
  "name": "dados",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
    { "type": "Feature", "properties": { "VINC": "6" }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -46.489342251452101, -23.66511439561954 ] ] } },
    { "type": "Feature", "properties": { "VINC": "1" }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -46.489439968502133, -23.665105357310239 ] ] } },
    { "type": "Feature", "properties": { "VINC": "0" }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -46.489537955929293, -23.665096808547311 ] ] } },
    { "type": "Feature", "properties": { "VINC": "0" }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -46.559119794597237, -23.653810429167063 ] ] } },
    { "type": "Feature", "properties": { "VINC": "0" }, "geometry": { "type": "MultiPoint", "coordinates": [ [ -46.489140609582904, -23.665134091460494 ] ] } }
  ]
}

var centroids_hm = geoJson2heat(json_dados_centroids, 'VINC');
    
var layer_centroids = new L.heatLayer(centroids_hm, {
  attribution: '',
  interactive: true,
  radius: 30,
  max: 11917,
  minOpacity: 1,
  gradient: {0: '#fcfdbf', 0.019608: '#fcf4b6', 0.039216: '#fdebac', 0.0588942: '#fde2a3', 0.078431: '#fed89a', 
    0.098039: '#fecf92', 0.117647: '#fec68a', 0.137255: '#febd82', 0.156863: '#feb47b', 
    0.176471: '#feaa74', 0.196078: '#fea16e', 0.215686: '#fd9869', 0.235294: '#fc8e64', 
    0.254902: '#fb8560', 0.27451: '#f97b5d', 0.294118: '#f7725c', 0.313725: '#f4695c', 
    0.333333: '#f1605d', 0.352941: '#ec5860', 0.372549: '#e75263', 0.392157: '#e04c67', 
    0.411765: '#d9466b', 0.431373: '#d2426f', 0.45098: '#ca3e72', 0.470588: '#c23b75', 
    0.490196: '#ba3878', 0.509804: '#b2357b', 0.529412: '#aa337d', 0.54902: '#a1307e', 
    0.568627: '#992d80', 0.588235: '#912b81', 0.607843: '#892881', 0.627451: '#812581', 
    0.647059: '#792282', 0.666667: '#721f81', 0.686275: '#6a1c81', 0.705882: '#621980', 
    0.72549: '#5a167e', 0.745098: '#52137c', 0.764706: '#4a1079', 0.784314: '#420f75', 
    0.803922: '#390f6e', 0.823529: '#311165', 0.843137: '#29115a', 0.862745: '#21114e', 
    0.882353: '#1a1042', 0.901961: '#140e36', 0.921569: '#0e0b2b', 0.941176: '#090720', 
    0.960784: '#050416', 0.980392: '#02020b', 1: '#000004'}
  }
);
       
layer_centroids.setData(centroids_hm);
            
function geoJson2heat(geojson, weight) {
  return geojson.features.map(function(feature) {
    return [
      feature.geometry.coordinates[1],
      feature.geometry.coordinates[0],
      feature.properties[weight]
    ];
  });
} 
    
map.addLayer(layer_centroids);

【问题讨论】:

    标签: leaflet heatmap qgis


    【解决方案1】:

    首先,您的 GeoJSON 包含多点形式的几何图形。但是,在“geoJson2heat”中,您解析点。 我建议将几何图形转换为点(在 'json_dados_centroids' 变量中)。 还需要删除layer_centroids.setData(centroids_hm)

    类似的东西

    var json_dados_centroids = {
        "type": "FeatureCollection",
        "name": "dados",
        "crs": {
            "type": "name",
            "properties": {
                "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
            }
        },
        "features": [{
                "type": "Feature",
                "properties": {
                    "VINC": "6"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-46.489342251452101,
                        -23.66511439561954
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "VINC": "1"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-46.489439968502133,
                        -23.665105357310239
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "VINC": "0"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-46.489537955929293,
                        -23.665096808547311
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "VINC": "0"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-46.559119794597237,
                        -23.653810429167063
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "VINC": "0"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [-46.489140609582904,
                        -23.665134091460494
                    ]
                }
            }
        ]
    }
    
    var centroids_hm = geoJson2heat(json_dados_centroids, 'VINC');
    
    var layer_centroids = new L.heatLayer(centroids_hm, {
        attribution: '',
        interactive: true,
        radius: 30,
        max: 11917,
        minOpacity: 1,
        gradient: {
            0: '#fcfdbf',
            0.019608: '#fcf4b6',
            0.039216: '#fdebac',
            0.0588942: '#fde2a3',
            0.078431: '#fed89a',
            0.098039: '#fecf92',
            0.117647: '#fec68a',
            0.137255: '#febd82',
            0.156863: '#feb47b',
            0.176471: '#feaa74',
            0.196078: '#fea16e',
            0.215686: '#fd9869',
            0.235294: '#fc8e64',
            0.254902: '#fb8560',
            0.27451: '#f97b5d',
            0.294118: '#f7725c',
            0.313725: '#f4695c',
            0.333333: '#f1605d',
            0.352941: '#ec5860',
            0.372549: '#e75263',
            0.392157: '#e04c67',
            0.411765: '#d9466b',
            0.431373: '#d2426f',
            0.45098: '#ca3e72',
            0.470588: '#c23b75',
            0.490196: '#ba3878',
            0.509804: '#b2357b',
            0.529412: '#aa337d',
            0.54902: '#a1307e',
            0.568627: '#992d80',
            0.588235: '#912b81',
            0.607843: '#892881',
            0.627451: '#812581',
            0.647059: '#792282',
            0.666667: '#721f81',
            0.686275: '#6a1c81',
            0.705882: '#621980',
            0.72549: '#5a167e',
            0.745098: '#52137c',
            0.764706: '#4a1079',
            0.784314: '#420f75',
            0.803922: '#390f6e',
            0.823529: '#311165',
            0.843137: '#29115a',
            0.862745: '#21114e',
            0.882353: '#1a1042',
            0.901961: '#140e36',
            0.921569: '#0e0b2b',
            0.941176: '#090720',
            0.960784: '#050416',
            0.980392: '#02020b',
            1: '#000004'
        }
    });
    
    function geoJson2heat(geojson, weight) {
        return geojson.features.map(function(feature) {
            return [
                feature.geometry.coordinates[1],
                feature.geometry.coordinates[0],
                feature.properties[weight]
            ];
        });
    }
    
    map.addLayer(layer_centroids);
    

    附: "VINC": "6" - 奇怪的强度值。

    【讨论】:

    • 谢谢,将多点转换为点,我可以看到地图了。还没有以合法的方式。关于 VINC 6 的值,该字段包含的数量最多为 300。由于我是由 qgis 生成的,我不知道代码中的强度应该在 0 和 1 之间。我用另一个带有热图的示例对其进行了调整.js 和leaflet-heatmap.js,它变得更好了。
    猜你喜欢
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多