GeoJSON issue (ol.source.GeoJSON is not a constructor)

1,467 views
Skip to first unread message

Sebastien Blanc

unread,
Aug 28, 2015, 6:40:55 AM8/28/15
to GeoAdmin API
Dear all,
I am trying to implement a GeoJSON overlay on my map where I would read the vector information from a json var. Here's an ol example that I am trying to implement but without any success:
http://openlayers.org/en/master/examples/geojson.html

Additionaly, I have the following error when trying another method as described below:

var map = new ga.Map({

target
: 'mymap',
view
: new ol.View({

resolution
: 500,
center
: [center_Y, center_X]
})
});
var lyr1 = ga.layer.create('ch.swisstopo.pixelkarte-farbe');
map
.addLayer(lyr1);

var vectorSource = new ol.layer.Vector({

source
: new ol.source.GeoJSON({

url
: 'getGeoJSON.php',
defaultProjection
:'EPSG:4326', projection: 'EPSG:3857'

}),
style
: new ol.style.Style({

stroke
: new ol.style.Stroke({

color
: 'green',
width
: 5
})
})
});

var vectorLayer = new ol.layer.Vector({

source
: vectorSource
});
console
.log(vectorLayer.getSource().getExtent()); // This returns infinite boundaries

map
.addLayer(vectorLayer);



This implementation returns me a "ol.source.GeoJSON is not a constructor", and after some research, it should be. Does the geo admin api use an old OL API?

I tried another implementation where I read the JSON var:

var jsonObj = <?php include('getGeoJSON.php') ?>;
var vectorSource = new ol.source.Vector({

format
: new ol.format.GeoJSON(),
projection
: 'EPSG:3857',
source
: (new ol.format.GeoJSON()).readFeatures(jsonObj, 'EPSG:3857')
});



But I get inifinite boundaries for the Extent() of the source, meaning the the json was not well interpreted.

Thank you in advance for your help!

Gilbert Jeiziner

unread,
Aug 28, 2015, 8:30:25 AM8/28/15
to geoadm...@googlegroups.com, Sebastien Blanc
Hi Sebastien,

the current API uses OpenLayers 3.6. Support for ol.source.GeoJSON has
been removed with
OpenLayers 3.5. So it can't be used anymore.

Anyhow, it should be possible to use GEOJson with our API. The page you
referred http://openlayers.org/en/master/examples/geojson.html shows a
working example.

In addition, we have on our examples page an example as well which uses
GEOJson as datasource:
http://api3.geo.admin.ch/examples/geoadmin_geojson.html
Here is it's source code:
http://api3.geo.admin.ch/examples/geoadmin_geojson.js

I hope this helps.

Gilbert
> --
> You received this message because you are subscribed to the Google
> Groups "GeoAdmin API" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to geoadmin-api...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout [1].
>
>
> Links:
> ------
> [1] https://groups.google.com/d/optout

Sebastien Blanc

unread,
Sep 1, 2015, 10:38:28 AM9/1/15
to GeoAdmin API, se.b...@gmail.com
Dear Gilbert,
Thank you for your reply. The example helped me to make it work. My main issue was that the coordinates I have for my path (lineString) is in WGS84 whereas it seems that MN03 should be used for the GeoJSON file. I proceed to a conversion point by point using the json utility of the swisstopo website: http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/products/software/products/m2m/wgs84tolv03.html
This takes some time since I have arround 200 points and I send one request par point. Is thereĀ  another way to proceed? Can't I configure my json reading to be in WGS84 instead of MN03 ?

Thank you in advance,


SƩbastien Blanc

Gilbert Jeiziner

unread,
Sep 2, 2015, 2:22:17 AM9/2/15
to geoadm...@googlegroups.com, se.b...@gmail.com
Hi SƩbastien,

for our API, we only support LV03 for GEOJson files. There are
open-source
tools available that will transform a GEOJson file from WGS84 to LV03.
For
some of them, see [1].

Anyhow, if you have your data in WGS84, there's also the possibility to
create
a kml file. KML is WGS84 only and is well supported by our API.

I hope this helps.

Gilbert

[1]
http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/products/software/products/skripts.html

On 01.09.2015 16:38, Sebastien Blanc wrote:
> Dear Gilbert,
> Thank you for your reply. The example helped me to make it work. My
> main issue was that the coordinates I have for my path (lineString) is
> in WGS84 whereas it seems that MN03 should be used for the GeoJSON
> file. I proceed to a conversion point by point using the json utility
> of the swisstopo website:
> http://www.swisstopo.admin.ch/internet/swisstopo/fr/home/products/software/products/m2m/wgs84tolv03.html
> This takes some time since I have arround 200 points and I send one
> request par point. Is there another way to proceed? Can't I configure
> my json reading to be in WGS84 instead of MN03 ?
>
> Thank you in advance,
>
> SƩbastien Blanc
>
> On Friday, August 28, 2015 at 2:30:25 PM UTC+2, Gilbert Jeiziner
> wrote:
>
>> Hi Sebastien,
>>
>> the current API uses OpenLayers 3.6. Support for ol.source.GeoJSON
>> has
>> been removed with
>> OpenLayers 3.5. So it can't be used anymore.
>>
>> Anyhow, it should be possible to use GEOJson with our API. The page
>> you
>> referred http://openlayers.org/en/master/examples/geojson.html [1]
>> shows a
>> working example.
>>
>> In addition, we have on our examples page an example as well which
>> uses
>> GEOJson as datasource:
>> http://api3.geo.admin.ch/examples/geoadmin_geojson.html [2]
>> Here is it's source code:
>> http://api3.geo.admin.ch/examples/geoadmin_geojson.js [3]
>>
>> I hope this helps.
>>
>> Gilbert
>>
>> On 28.08.2015 12:40, Sebastien Blanc wrote:
>>> Dear all,
>>> I am trying to implement a GeoJSON overlay on my map where I would
>>
>>> read the vector information from a json var. Here's an ol example
>> that
>>> I am trying to implement but without any success:
>>> http://openlayers.org/en/master/examples/geojson.html [1]
>>> For more options, visit https://groups.google.com/d/optout [4]
>> [1].
>>>
>>>
>>> Links:
>>> ------
>>> [1] https://groups.google.com/d/optout [4]
>
> --
> You received this message because you are subscribed to the Google
> Groups "GeoAdmin API" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to geoadmin-api...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout [4].
>
>
> Links:
> ------
> [1] http://openlayers.org/en/master/examples/geojson.html
> [2] http://api3.geo.admin.ch/examples/geoadmin_geojson.html
> [3] http://api3.geo.admin.ch/examples/geoadmin_geojson.js
> [4] https://groups.google.com/d/optout

Marc Monnerat

unread,
Sep 2, 2015, 4:45:49 AM9/2/15
to geoadmin-api
Hello Sebastien,

You may also convert your GeoJSON on-the-fly into EPSG:21781 (LV03/MN03), from, let say a EPSG:4326 source, like this:

var source = new ol.source.Vector({
Ā  url: 'path to WGS1984 GeoJSON',
Ā  format: new ol.format.GeoJSON({
Ā  Ā  // Default data projection, if cannot be derived from data itself
Ā  Ā  defaultDataProjection: 'EPSG:4326'
Ā  })
});

Note, projection like EPSG:4326, EPSG:3857, EPSG:21781 are either embeded in GeoAdmin API or OL3. If you use other projection, you have to add them.

Regards,

Marc

Reply all
Reply to author
Forward
0 new messages