1

I work on a GIS web app using GeoDjango and I struggle reading and displaying a multipoint geometry on my map.

If I serialize my GEOSGeometry object to GeoJSON, I can get the following JSON file :

{ "type": "MultiPoint", "coordinates": [ [ X0, Y0, Z0 ], [ X1, Y1, Z1 ], ... ]}

Now, when I want to read this data using readGeometry() as follows

var vectorSource = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readGeometry(geojson_object),
    });

I get the error SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data

I tried parsing my data using GeoJSONLint but it does seem correct.

So I tried to use readFeature() (as I only have one feature) as a matter of testing with slight modification to my geojson file :

{ "type": "Feature", "geometry" : { "type": "MultiPoint", "coordinates": [ [ X0, Y0, Z0 ], [ X1, Y1, Z1 ], ... ] } }

Then I get TypeError: n.features.getArray is not a function

Again, if I use readFeatures() on the same feature object, I get no error whereas it should expect a collection of features, right ?

What am I missing ? The openlayers documentation is not that clear to me.

1 Answer 1

4

The feature property in a vector source must be an array of features. Only readFeatures returns an array. Results from the other methods need to be made into an array of features

    features: (new ol.format.GeoJSON()).readFeatures(geojson_object),

    features: [ (new ol.format.GeoJSON()).readFeature(geojson_object) ],

    features: [ new ol.Feature((new ol.format.GeoJSON()).readGeometry(geojson_object)) ],
1
  • Crystal clear, thank you. Also, just for the record and if anybody still have a SyntaxError: JSON.parse: expected property name kind of error, and using Django templating, be sure to escape your variable with escapejs like so var data = "{{ variable.geom.json | escapejs }}" otherwise, you get some &quote making your parser fail.
    – Beinje
    Commented Feb 21, 2020 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.