Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoJSON layer remove/replace data #1416

Closed
tmcw opened this issue Feb 19, 2013 · 20 comments
Closed

GeoJSON layer remove/replace data #1416

tmcw opened this issue Feb 19, 2013 · 20 comments
Assignees
Milestone

Comments

@tmcw
Copy link
Contributor

tmcw commented Feb 19, 2013

The L.geoJson API seems to be missing an API that would make it symmetric:

var layer = L.geoJson();

// we have this
layer.addData(foo);

// we don't have this
layer.removeData(foo);

// or this
layer.getData(foo);
layer.replaceData(foo);

// and if we had this
layer.data(foo); // set the data to foo
layer.data(); // get all data

// we could implement the rest as sugar
@mourner
Copy link
Member

mourner commented Feb 19, 2013

Yeah, currently such use cases are handled like this:

layer.clearLayers(); // inherited from LayerGroup
layer.addData(newData);

So GeoJSON class currently acts like a one-way converter of data. But I agree that being able to remove specific objects would be useful.

@tmcw
Copy link
Contributor Author

tmcw commented Feb 19, 2013

This layers-being-layer-groups thing is a bit unclear to me: is there documentation of how it's supposed to work? Basically layers = features?

layer.clearLayers(); // should this be documented in the geojson layer docs, since it does something else here?

@mourner
Copy link
Member

mourner commented Feb 19, 2013

GeoJSON layer works exactly like a group, it just adds a couple of GeoJSON-related methods and overrides constructor to accept GeoJSON object. When you call addData, it converts objects to basic Leaflet layers and adds them to itself. When you call clearLayers, it just clears its layers (without any additional GeoJSON-related logic). So it's quite simple.

@mourner mourner closed this as completed Feb 19, 2013
@mourner mourner reopened this Feb 19, 2013
@paulovieira
Copy link
Contributor

Having the possibility to manipulate GeoJson data in Leaflet core would be a very useful feature. It should be a 2-way process.

I already suggested this some months ago: http://leaflet.uservoice.com/forums/150880-ideas-and-suggestions-for-leaflet/suggestions/3121234-convert-vectors-layers-to-geojson

@ghost ghost assigned mourner Feb 21, 2013
@emacsen
Copy link

emacsen commented Mar 6, 2013

I want to say that I too would benefit from this feature. Either being able to remove data, or being able to simply remove all the data from the geoJSON layer.

For a dynamic map where you are replacing the geoJSON data, this functionality would make things better.

@brianherbert
Copy link
Contributor

If I could, +1 on this. I'm currently pulling my hair out trying to manipulate / clear geojson data on the map without hacking around core functionality leaflet.

@JeffPaine
Copy link

👍

@MacWeber
Copy link

MacWeber commented Sep 9, 2013

layer.clearLayers();
is not working for clearing the L.geoJson layer.
I cannot figure out how to get rid of old data when I want to update with new GeoJSON data.

@MacWeber
Copy link

MacWeber commented Sep 9, 2013

I realize I was not on HEAD. layer.clearLayers(); is working fine.

@Xeonios
Copy link

Xeonios commented Oct 7, 2013

yep. It's Work on GeoJSON layer for me

@ThomasG77
Copy link

+1 to be able to remove one feature at a time and not only clear all.
Playing with a case where I use HTML5 Server Side Events to refresh every 2 sec the data to always keep 10 visible markers (remove the oldest, add the latest). With this example, rerendering everything is quite visible (by doing the clearLayers(); and adding again all features)
Better option maybe?

@andreamartelli
Copy link

I fully agree with all of the previous comments.
I used OpenLayers in the past, and in the switch to Leaflet I really enjoyed it's simplicity and mobile friendliness. BUT the lack of this feature is really bad IMHO. It would allow Leaflet to compete with OL also for real-time tracking scenarios and much more.
Developing an extension to the library would be so cool...

@perliedman
Copy link
Member

Note that it is possible to remove a single feature from a GeoJSON layer. As @mourner mentions above, L.GeoJSON inherits from L.LayerGroup, which supports removing single layers.

Add an onEachFeature handler for the GeoJSON layer to keep track of the layers associated with a feature (putting them in a map where the feature is the key and the layer is the value, for example), and then use L.LayerGroup's removeLayer to remove them.

A simplistic example of this: http://jsfiddle.net/MLuc3/3/

@mourner any particular reason this issue is still open? Any plans on reworking the API? I'd vore for closing.

@andreamartelli
Copy link

Thanks Per. What I'm probably missing is the understanding of the relationship between features and layers in a GeoJSON layer. Is it 1:1? If I add, say, 10 Points, to the layer with addData, do 10 layers get created in the LayerGroup?

@perliedman
Copy link
Member

@andreamartelli yes, exactly. For complex geometries, like a GeometryCollection, it will in fact return a FeatureGroup, where each subfeature has its own layer.

You can see how it's done in GeoJSON.js / geometryToLayer.

@tmcw
Copy link
Contributor Author

tmcw commented Mar 9, 2014

@perliedman yep, the core point is that this kind of interface is doable as an abstraction layer, and the current interface is all right. I'll make a note to do it at some point.

@volkanunsal
Copy link

layer.clearLayers() is not working for me on version 0.7.3. Is this feature only available on the master branch?

@volkanunsal
Copy link

Ah, never mind. Figured out that it is working.

@bgeils
Copy link

bgeils commented Dec 11, 2016

Here is the code to the solution I implemented. I made a dictionary of all the layers that I could look up and remove whenever necessary.

    var parcelDict = {};

    var queryParcel = Parcel.find();

    queryParcel.observe({
      added: function (item) { 
            parcelDict[item._id._str] = L.geoJson(item, {
                style: {
                    fillColor: "FFF", //overlay color
                    weight: 2,
                    opacity: 1,
                    color: "FFF", // border color
                    fillOpacity: 0.65
                },
                onEachFeature: onEachFeature
            }).addTo(mymap);
        },
      changed: function (newItem) {
          parcelDict[newItem._id._str].clearLayers();
          L.geoJson(newItem, {
                style: {
                    fillColor: "FFF", //overlay color
                    weight: 2,
                    opacity: 1,
                    color: "FFF", // border color
                    fillOpacity: 0.65
                },
                onEachFeature: onEachFeature
            }).addTo(mymap);
        }   
    });

@rowilsonh
Copy link

I think leaflet-ajax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests