I can't seem to get multiple polygons from GeoJSON or KML (through Geocoder) to appear on a Leaflet map. Only the first polygons get rendered on the map.

Modules installed: Geofield and Geofield map (7.x-1.1 and 7.x-1.x-dev), Leaflet, Geocoder
Steps done:

  1. Added 2 Geofields to a content type. Specified GeoJSON as widget types for both.
  2. Specified formats as "Leaflet" for the first field and "Geofield map" for the second field in the manage display screen.
  3. Added the same GeoJSON data to both fields in a new node: {"type":"MultiPolygon","coordinates":[[[[74.92,37.24],[74.57,37.03],[72.56,36.82],[71.24,36.13],[71.65,35.42],[71.08,34.06],[69.91,34.04],[70.33,33.33],[69.51,33.03],[69.33,31.94],[66.72,31.21],[66.26,29.85],[62.48,29.41],[60.87,29.86],[61.85,31.02],[60.84,31.5],[60.58,33.07],[60.94,33.52],[60.51,34.14],[61.28,35.61],[62.72,35.25],[63.12,35.86],[64.5,36.28],[64.8,37.12],[66.54,37.37],[67.78,37.19],[69.32,37.12],[70.97,38.47],[71.59,37.9],[71.68,36.68],[73.31,37.46],[74.92,37.24]]],[[[19.44,41.02],[19.37,41.85],[19.65,42.62],[20.07,42.56],[20.59,41.88],[20.82,40.91],[20.98,40.86],[20.01,39.69],[19.29,40.42],[19.44,41.02]]],[[[2.96,36.8],[8.62,36.94],[8.18,36.52],[8.25,34.64],[7.49,33.89],[9.06,32.1],[9.54,30.23],[9.95,27.82],[9.87,26.51],[9.4,26.15],[10.25,24.61],[11.56,24.3],[11.99,23.52],[5.81,19.45],[4.25,19.15],[3.33,18.98],[3.23,19.82],[1.8,20.31],[-4.81,25],[-6.66,26.13],[-8.67,27.29],[-8.67,27.67],[-8.67,28.71],[-3.63,30.97],[-3.82,31.7],[-1.18,32.11],[-1.75,34.75],[-2.21,35.09],[0.95,36.45],[2.96,36.8]]]]}
  4. When saved, the first field only displays one polygon. The second field displays all three.

Same thing seems to happen for KML files.

Comments

gumdal’s picture

Hi,

I have been trying to draw KML polygons on the map. I do not know how or why am not at all able to get it draw on the map. Please share with me about how to do it? Or if you have any guide and steps to do it will be helpful.

arcsump’s picture

Hi!
Just opened leaflet.drupal.js
The way i see it it is not implemented as desired?
hunted the codes for that.
Hope this helps:

function leaflet_create_feature(feature) {
        var lFeature;
        switch (feature.type) {
          case 'point':
            lFeature = Drupal.leaflet.create_point(feature);
            break;
          case 'linestring':
            lFeature = Drupal.leaflet.create_linestring(feature);
            break;
          case 'polygon':
            lFeature = Drupal.leaflet.create_polygon(feature);
            break;
          case 'multipolygon':
          case 'multipolyline':
            lFeature = Drupal.leaflet.create_multipoly(feature);
            break;
          case 'json':
            lFeature = Drupal.leaflet.create_json(feature.json)
            break;
        }

create_multipoly: function(multipoly) {
      var polygons = [];
      for (var x = 0; x < multipoly.component.length; x++) {
        var latlngs = [];
        var polygon = multipoly.component[x];
        for (var i = 0; i < polygon.points.length; i++) {
          var latlng = new L.LatLng(polygon.points[i].lat, polygon.points[i].lon);
          latlngs.push(latlng);
          this.bounds.push(latlng);
        }
        polygons.push(latlngs);
      }
      if (multipoly.multipolyline) {
        return new L.MultiPolyline(polygons);
      }
      else {
        return new L.MultiPolygon(polygons);
      }
    },


rdeboer’s picture

It works if you enter your MultiPolygon as a MultiLineString, which in the current code are treated equally, in that Leaflet closes the MultiLineString (thus turning it into a polygon) and then fills in the area with a blue shading, as is common for a polygon.
The example in pont 3 above thus becomes:

{"type":"MultiLineString","coordinates":[
[[74.92,37.24],[74.57,37.03],[72.56,36.82],[71.24,36.13],[71.65,35.42],[71.08,34.06],[69.91,34.04],[70.33,33.33],[69.51,33.03],[69.33,31.94],[66.72,31.21],[66.26,29.85],[62.48,29.41],[60.87,29.86],[61.85,31.02],[60.84,31.5],[60.58,33.07],[60.94,33.52],[60.51,34.14],[61.28,35.61],[62.72,35.25],[63.12,35.86],[64.5,36.28],[64.8,37.12],[66.54,37.37],[67.78,37.19],[69.32,37.12],[70.97,38.47],[71.59,37.9],[71.68,36.68],[73.31,37.46],[74.92,37.24]],

[[19.44,41.02],[19.37,41.85],[19.65,42.62],[20.07,42.56],[20.59,41.88],[20.82,40.91],[20.98,40.86],[20.01,39.69],[19.29,40.42],[19.44,41.02]],

[[2.96,36.8],[8.62,36.94],[8.18,36.52],[8.25,34.64],[7.49,33.89],[9.06,32.1],[9.54,30.23],[9.95,27.82],[9.87,26.51],[9.4,26.15],[10.25,24.61],[11.56,24.3],[11.99,23.52],[5.81,19.45],[4.25,19.15],[3.33,18.98],[3.23,19.82],[1.8,20.31],[-4.81,25],[-6.66,26.13],[-8.67,27.29],[-8.67,27.67],[-8.67,28.71],[-3.63,30.97],[-3.82,31.7],[-1.18,32.11],[-1.75,34.75],[-2.21,35.09],[0.95,36.45],[2.96,36.8]]
]}

Note that a MultiLineString doesn't have the extra pair of enclosing brackets for each of its LineStrings, whereas a MultiPolygon does.

mgifford’s picture

Any progress on this issue?

dshields’s picture

I guess the solution in #3 works (haven't tested it yet), but what I'm looking to do is use geofield to generate this data from another field, a file field that holds a kml file..
Can anyone provide a solution on how to change the "MultiPolygon" to a "MultiLineString" and alter the brackets accordingly when using geofield's "Geocode from another field" feature, as described here: https://drupal.org/node/1980360?

dshields’s picture

Looks like this one has really stalled.

balagan’s picture

Issue summary: View changes
StatusFileSize
new1.2 KB

I have enclosed a patch for this. It is working for me with the given geoJSON string.

balagan’s picture

StatusFileSize
new1.57 KB

I have just noticed, that an if-else statement is not necessary, since there is no fallthrough in the switch anymore. I have corrected the patch accordingly.

balagan’s picture

Assigned: Unassigned » balagan
Status: Active » Needs work

Another patch got committed into this file, so I will have to reroll the patch.

balagan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.54 KB

I have attached the rerolled patch.

gcb’s picture

Status: Needs review » Fixed

Great patch @balagan, thanks. Much cleaner separating out the switch statements for those different element types, too. Committed to Dev, look for it in the upcoming release.

balagan’s picture

Assigned: balagan » Unassigned
gcb’s picture

Status: Fixed » Closed (fixed)