简体   繁体   English

通过Leaflet中来自csv的数据定义json多边形的颜色

[英]Define json polygon's color by data from csv in Leaflet

I want to do something that i thought would be pretty easy but i face a problem. 我想做一些我认为很容易的事情,但是我遇到了问题。

I have two files : a geojson file with geometry and a csv with two columns, one for data and an another which shares attribute with a propertie in the geojson, a name to be simple. 我有两个文件:一个带有几何图形的geojson文件和一个带有两列的csv,一个用于数据,另一个用于在geojson中共享具有属性的属性,这个name很简单。

I want to make a choropleth map depending of data from my csv to fill my geojson polygon. 我想根据我的csv中的数据制作一个Choropleth地图,以填充我的geojson多边形。 I check the leaflet documentation for choropleth map with json and i follow, in part, this answer for communication between json and csv in leaflet. 我检查了带json的Choropleth映射传单文档,并且部分遵循了有关传单中json和csv之间通信的答案

So, this is a part of my code which is problematic, json polygons are mapped but colors are not define. 因此,这是我的代码的一部分,这是有问题的,已映射json多边形,但未定义颜色。 I learn JS so sorry for the mistakes : 我学习JS,因此对不起您的错误:

EDIT : fix typo 编辑 :修正错别字

Papa.parse("score.csv", {
    download: true,
    header: true,
    delimiter: "",
    complete: function(results) { //everything below runs only after the CSV has been loaded and processed.

        function getColor(d) {
            return d > 0.5 ? '#800026' :
                   d > 0.4  ? '#BD0026' :
                   d > 0.3  ? '#E31A1C' :
                   d > 0.25  ? '#FC4E2A' :
                   d > 0.2   ? '#FD8D3C' :
                   d > 0.15   ? '#FEB24C' :
                   d > 0.1   ? '#FED976' :
                              '#FFEDA0';
        }

        var oiseLayer = new L.geoJson(oise, {
                style: function(feature){
                    // var filtered = results.data.filter(function(data){return data.DCOMIRIS == this;}, feature.properties.DCOMIRIS.toString());

                    if (results.data.DCOMIRIS == feature.properties.DCOMIRIS){
                    return {
                        weight: 1,
                        opacity: 1,
                        color: 'white',
                        fillOpacity: 0.7,
                        fillColor: getColor(results.data.score)
                    }
                }
                }
           }).addTo(map);
}
});

My json looks like this : 我的json看起来像这样:

var oise = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },                                                                                
"features": [
{ "type": "Feature", "properties": { "DEPCOM": "60538", "NOM_COM": "Ricquebourg", "IRIS": "0000", "DCOMIRIS": "605380000", "NOM_IRIS": "Ricquebourg", "TYP_IRIS": "Z" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.753862487322691, 49.568106423004863 ], [ 2.754834569610532, 49.567467723103867 ], [ 2.755374727888817, 49.567112846570275 ], [ 2.75585282747064, 49.566527871018629 ], [ 2.755916398961054, 49.56594385787055 ], [ 2.755844137158009, 49.565449439131314 ], [ 2.755354284133803, 49.564733104021172 ], [ 2.756729896258653, 49.564817378618457 ], [ 2.758105512897535, 49.564901637620451 ],...

And my csv : 而我的csv:

DCOMIRIS;score
600010000;0.025
600020000;0.03333333333333333

Besides, i got an error in my console which says that my csv transform in json is "not well-formed". 此外,我的控制台出现错误,提示我在json中的csv转换“格式不正确”。

Thanks 谢谢

You have a typo in if (result.data.DCOMIRIS == feature.properties.DCOMIRIS){ => should be results with s to match your function argument identifier. 您有一个错字, if (result.data.DCOMIRIS == feature.properties.DCOMIRIS){ =>应该是带有s的 results ,以匹配您的函数参数标识符。

But this is not the core of your issue. 但这不是您问题的核心。

results.data is an array of items (one per line in your CSV file, except for the first / header line). results.data是一组项目(CSV文件中的每行一个,第一行/标题行除外)。 So the if condition " result.data.DCOMIRIS == feature.properties.DCOMIRIS " will never be met. 因此, if条件“ result.data.DCOMIRIS == feature.properties.DCOMIRIS ”将永远不会得到满足。

You should rather map your DCOMIRIS codes in an object, so that you can easily refer to these codes when parsing the GeoJSON data and trying to determine the appropriate style / color. 您应该将DCOMIRIS代码映射到一个对象中,以便在解析GeoJSON数据并尝试确定适当的样式/颜色时可以轻松地引用这些代码。 See my answer in that other question: Leaflet GeoJSON zoom to marker by id 看到我在另一个问题中的回答: Leaflet GeoJSON缩放到按ID标记

// map the DCOMIRIS first, so that we can retrieve them by DCOMIRIS easily.
var DCOMIRIS = {},
  data = results.data;

for (var i = 0; i < data.length; i += 1) {
  DCOMIRIS[data[i].DCOMIRIS] = data[i].score;
}

Then you can retrieve the score of your feature by calling DCOMIRIS[feature.properties.DCOMIRIS] 然后,您可以通过调用DCOMIRIS[feature.properties.DCOMIRIS]来检索功能score DCOMIRIS[feature.properties.DCOMIRIS]

Then, I think PapaParse gives you only strings, so you will have to convert the score to a float: parseFloat(score) 然后,我认为PapaParse仅给您字符串,因此您必须将score转换为浮点数: parseFloat(score)

Finally you can use your getColor function: 最后,您可以使用getColor函数:

var oiseLayer = L.geoJson(oise, {
  style: function(feature) {
    return {
      weight: 1,
      opacity: 1,
      color: 'white',
      fillOpacity: 0.7,
      fillColor: getColor(parseFloat(DCOMIRIS[feature.properties.DCOMIRIS]))
    };
  }
}).addTo(map);

Demo: http://jsfiddle.net/ve2huzxw/185/ 演示: http//jsfiddle.net/ve2huzxw/185/

NOTE: not sure about your delimiter: "" , that may explain your console error. 注意:不确定您的delimiter: ""可能解释您的控制台错误。 Why not leaving the delimiter automatic detection? 为什么不离开定界符自动检测?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM