Skip to content

Commit

Permalink
fix(layers): passing a geojson object should work
Browse files Browse the repository at this point in the history
After ol3 v3.5 there has been a breaking change in how
the GeoJSON objects are treated. This commit fixes
the functionality to be able to pass an eagerly loaded
GeoJSON object and not just through the "url" prop.

Example:

```
var geoJsonLayer = {
  source: {
    type: 'GeoJSON',
    geojson: {
       object: {
          type: 'Feature',
          geometry: {
            type: 'Point',
            coordinates: [
              11.3243,
              46.508745
            ]
          }
       },
       projection: '...'
    }
  },
  style: {
     ...
  }
}

```
  • Loading branch information
juristr committed Aug 22, 2015
1 parent e664a6a commit da28478
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/services/olHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,17 @@ angular.module('openlayers-directive').factory('olHelpers', function($q, $log, $
url: source.url
});
} else {
if (!isDefined(source.geojson.projection)) {
source.geojson.projection = projection;
oSource = new ol.source.Vector();

var projectionToUse = projection;
if (isDefined(source.geojson.projection)) {
projectionToUse = source.geojson.projection;
}
oSource = new ol.source.Vector(angular.extend(source.geojson, {
format: new ol.format.GeoJSON()
}));

var geojsonFormat = new ol.format.GeoJSON();
var features = geojsonFormat.readFeatures(source.geojson.object, { featureProjection: projectionToUse });

oSource.addFeatures(features);
}

break;
Expand Down
38 changes: 38 additions & 0 deletions test/unit/layersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ describe('Directive: openlayers layers', function() {
expect(layers.item(1).getSource() instanceof ol.source.TileJSON).toBe(true);
});

it('should properly render a GeoJSON layer containing the GeoJSON object', function(){
scope.geoJsonLayer = {
source: {
type: 'GeoJSON',
geojson: {
object: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
11.32436509382857,
46.50874575613664
]
}
}
}
}
};

var element = angular.element('<openlayers>' +
'<ol-layer ol-layer-properties="geoJsonLayer"></ol-layer>' +
'</openlayers>');
element = $compile(element)(scope);

var layers;
olData.getMap().then(function(olMap) {
layers = olMap.getLayers();
});

scope.$digest();
expect(layers.item(0).getSource() instanceof ol.source.OSM).toBe(true);
expect(layers.getLength()).toBe(2);

var geoJsonLayer = layers.item(1);
expect(geoJsonLayer.getSource() instanceof ol.source.Vector).toBe(true);
expect(geoJsonLayer.getSource().getFeatures().length).not.toBe(0);
});

it('should have one layer if custom-layers is used', function() {
scope.mapbox = {
source: {
Expand Down

0 comments on commit da28478

Please sign in to comment.