Leaflet / Leaflet.VectorGrid

Display gridded vector data (sliced GeoJSON or protobuf vector tiles) in Leaflet 1.0.0
598 stars 194 forks source link

An error caused when using GeomType = 0 (UNKNOWN) #162

Open kkdd opened 6 years ago

kkdd commented 6 years ago

Hello, When using a tile with GeomType = 0 (UNKNOWN), L.vectorGrid.protobuf says

Leaflet.VectorGrid.js:113 Uncaught (in promise) TypeError: Cannot read property 'render' of undefined

But I think L.vectorGrid.protobuf should regard it as no content as MapboxGL does.

kkdd commented 6 years ago

The following is a quick hack:

--- Leaflet.VectorGrid.js.orig
+++ Leaflet.VectorGrid.js
@@ -113,6 +113,9 @@
                        }

                        var featureLayer = this._createLayer(feat, pxPerExtent);
+                       if (!featureLayer) {
+                           continue;
+                       }

                        for (var j = 0; j < styleOptions.length; j++) {
                            var style = L.extend({}, L.Path.prototype.options, styleOptions[j]);
@@ -226,7 +229,7 @@
            break;
        }

-       if (this.options.interactive) {
+       if (!layer && this.options.interactive) {
            layer.addEventParent(this);
        }
IvanSanchez commented 6 years ago

How are you generating that tile? Or, could you link to such a tile? It'd be good to be able to reproduce this locally.

kkdd commented 6 years ago

You can run https://github.com/kkdd/go-vtile-example/blob/master/main.go with the following modification (see a line of featureType = vector_tile.Tile_UNKNOWN):

func createTileWithPoints(points []XY, bounds XYZ) ([]byte, error) {
    layerName := "points"
    var layerVersion = vector_tile.Default_Tile_Layer_Version
    featureType := vector_tile.Tile_POINT
    var extent = vector_tile.Default_Tile_Layer_Extent
    var geometry []uint32
    geometry = append(geometry, 0)  // dummy
    var pX int32
    var pY int32
    x, y := tileToBoundingBox(bounds)
    for _, point := range points {
        if point.x >= x[0] && point.x < x[1] && point.y >= y[0] && point.y < y[1] {
            p := locToTileXY(point, bounds)
            deltaX := int32(float64(extent)*p.x+0.5) - pX
            deltaY := int32(float64(extent)*p.y+0.5) - pY
            geometry = append(geometry, uint32(paramEnc(deltaX)))
            geometry = append(geometry, uint32(paramEnc(deltaY)))
            pX = pX + deltaX
            pY = pY + deltaY
        }
    }
    npoints := (uint32(len(geometry))-1)/2
    geometry[0] = moveTo(npoints)
        if npoints == 0 {
                featureType = vector_tile.Tile_UNKNOWN
        }
    tile := &vector_tile.Tile{}
    tile.Layers = []*vector_tile.Tile_Layer{
        &vector_tile.Tile_Layer{
            Version: &layerVersion,
            Name:   &layerName,
            Extent:  &extent,
            Features: []*vector_tile.Tile_Feature{
                &vector_tile.Tile_Feature{
                    Tags:    []uint32{},
                    Type:    &featureType,
                    Geometry: geometry,
                },
            },
        },
    }
    return proto.Marshal(tile)
}