0

I created my geojson layer like this:

    var gjlayer = new ol.layer.Vector({
        title: 'My Layer',
        source: new ol.source.Vector({
            url: '/path-to-my-geojson-file',
            format: new ol.format.GeoJSON()
        })
    });

It's got properties on it that I change that I want to change when I click a polygon that was drawn from the gjlayer:

    var feature = mc.map.forEachFeatureAtPixel(evt.pixel,
      function(feature, layer) {
        return feature;
      });
    if (feature) {
      feature.set('newproperty', 'new');
    };

The problem is, this 'feature.set' does not appear to actually change gjlayer. If I log out the feature and attempt to style it onclick it does change the color based on the property, it appears to work, but the problem is the same polygon that is in gjlayer appears to remain the same... so if I clear the map and 'redraw' gjlayer, the styling and the property are not there.

I expect that if I cleared the map, and 'readded' the gjlayer, the feature that had the 'newproperty' would be there.

Rolando
  • 58,640
  • 98
  • 266
  • 407

2 Answers2

0

What do you mean, 'clear the map', do you create a new one? As far as i know your vectorlayer only saves the information during the time its added to the map-object. I think you will lose the information if you would create a new one or attach/detach the vectorlayer to the mapobject.

Update: Regarding OP's comment:

//Get Current Features from old vector layer
var oldFeatures = oldVectorLayer.getSource().getFeatures();
//Create a new source and add oldFeatures to this source
var newVectorLayerSource = new ol.source.Vector({
    features: oldFeatures
 });
//create new vector layer and add new vector Source
var newVectorLayer = new ol.layer.Vector({
    source: newVectorLayerSource
});
//if your vectorlayer already exist you can use set Source,
//but the old source will be overwritten
alreadyExistingVectorLayer.setSource(newVectorLayerSource);
//If you want to add the feature to an existing source use:
alreadyExistingVectorLayer.getSource().addFeatures(oldFeatures)

For further help i need more information what you wanna do :-) I think your problem can be saved easier with some eventlistener on your Vectorsource collection.

foedchr
  • 123
  • 1
  • 9
  • Yes, I create a new one and 're-add' the gjlayer to the map. I want the gjlayer on that new map to already have 'newproperty' in it. Are you saying I'd have to make a 'copy' of the vectorlayer on the map into a new vectorsource after i add the newproperty on click? If so, how do I do that? – Rolando Apr 10 '18 at 15:24
  • If you add a new vector layer with the same source as above in your Question the layer will be fetching the features from the given url/path and read the features in again. If you change a feature property it is in your current map/layer state changed, but it won't get written in to the filesystem. I will updated my answer regarding exchanging the vectorsource. – foedchr Apr 11 '18 at 08:34
  • In the example in my OP where I changed ONE feature in the gjlayer (vectorsource). Is what your saying that the map's vectorlayer changed, but not the original gjlayer? If so, then how do I access "oldVectorLayer"? Is it something like map.layers.oldVectorLayer or something? – Rolando Apr 12 '18 at 03:02
0

If you want to save the changes made to your GeoJSON layer you need to use WFS Transaction and probably you need a MapServer or GeoServer.

pavankguduru
  • 315
  • 1
  • 13
  • Assume no MapServer or GeoServer is being used to serve up the GeoJSON. the GeoJSON is just a hardcoded variable in the javascript. – Rolando Apr 18 '18 at 21:30
  • The only way I could think of is getting the updated features using `getFeatures()` and `writeFeatures()` as GeoJSON after the parameters are updated and overwrite your GeoJSON source. – pavankguduru Apr 19 '18 at 09:06