JavaScript Convert GeoJSON polygon to point before drawing in Leaflet Map geojson convert polygon to linestring,geojson convert polygon to point,

I have a GeoJSON dataset which has points and polygons. I have a simple Leaflet code which reads them into a map, like this:

var MyLayer = new L.GeoJSON.AJAX("/UrbanSyntax/Desarrollo/twitter    /data/boxtest.json", {

pointToLayer: function(feature, latlng) {      
return new L.CircleMarker(latlng, {
  radius: 3,
  fillOpacity: 0.75,
  color: getColor(feature.properties.created_at)
});
},
onEachFeature: function(feature, layer) {
layer.bindPopup(
  feature.properties.created_at + '<br />'
  + feature.geometry.coordinates + '<br />'
  + feature.properties.user  )
}
});

Most of the data are polygons, but I need to translate them to points (the polygon center) to simplify the map. I don't want to change the original GeoJSON when it's parsed since those polygons might be needed later.

I don't know where to "inject" code to read the polygon bounds, calculate a center and send a latlng to make a circlemarker. As it is now, the code reads the points and polygons in the json ok, but there are too many polygons in the data so the browser freezes. It works all right when I filter out the polys from the JSON and just map the points. I'm running out of ideas, and Leaflet documentation is so scarce in the JSON chapter... I just need a way to put an if in the pointToLayer code, separate the points from the polys, and map them all as points.

Thanks in advance!

Any ideas?

Answer:1

There does not appear to be a suitable hook.

At first glance, it seems that you might be able to mutate the GeoJSON that's passed to the style option's function, but Leaflet has already created its own feature/layer by that stage, so any mutation no effect.

To solve the problem, you could perform your own network request and modify the the data before it is passed in to the GeoJSON layer.

If you are targeting modern browsers, you could use fetch or one of its polyfills:

fetch('/UrbanSyntax/Desarrollo/twitter/data/boxtest.json')
.then(function (response) {
    return response.json();
})
.then(function (geoJson) {
  geoJson.features.forEach(function (feature) {
    if (feature.geometry.type === "Polygon") {
      // Copy the polygon geometry and replace it with a
      // point at the polygon's centroid.
      feature.polygonGeometry = feature.geometry;
      feature.geometry = {
        coordinates: getCentroid(feature.polygonGeometry),
        type: "Point"
      };
    }
  });
  geoJsonLayer = new L.GeoJSON(geoJson, {
    pointToLayer: function(feature, latlng) {      
      return new L.CircleMarker(latlng, {
        radius: 3,
        fillOpacity: 0.75,
        color: getColor(feature.properties.created_at)
      });
    },
    onEachFeature: function(feature, layer) {
      layer.bindPopup(
        feature.properties.created_at + '<br />'
        + feature.geometry.coordinates + '<br />'
        + feature.properties.user);
    }
  });
  geoJsonLayer.addTo(map);
});

Where getCentroid is some function that determines the centroid/center or whatever you wish to use.

Answer:2

I am trying to create a D3 sunburst chart based on this example: https://bl.ocks.org/maybelinot/5552606564ef37b5de7e47ed2b7dc099 I'd like the arcs created to close the full circle but they don't. ...

I am trying to create a D3 sunburst chart based on this example: https://bl.ocks.org/maybelinot/5552606564ef37b5de7e47ed2b7dc099 I'd like the arcs created to close the full circle but they don't. ...

I am new to css especially combinators but I am trying to change css of a p element when hovering over a h1 element both of which are in a div of class "a" but I can't figure out how to do it.Here is ...

I am new to css especially combinators but I am trying to change css of a p element when hovering over a h1 element both of which are in a div of class "a" but I can't figure out how to do it.Here is ...

  1. change class when clicked
  2. change class when scroll
  3. change class when hover
  4. css change class when hover
  5. jquery change class when click

new Date(Infinity) is too far, and so is new Date(Number.MAX_SAFE_INTEGER - 1). I need this so I can run astronomical simulations on a Node cluster. JK, I'm just messing around, and SO imposed a ...

new Date(Infinity) is too far, and so is new Date(Number.MAX_SAFE_INTEGER - 1). I need this so I can run astronomical simulations on a Node cluster. JK, I'm just messing around, and SO imposed a ...

I'm writing a lua script that I want to use with scrapy + splash for a website. I want to write a script that enters a text and then clicks on a button. I have the following code: function main(...

I'm writing a lua script that I want to use with scrapy + splash for a website. I want to write a script that enters a text and then clicks on a button. I have the following code: function main(...