1

How do I get GeoJSON.write to maintain the projections used in the construction of the GeoJSON?

points.geojson

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "carey",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -93.9001,
          49.213196
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "dan",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -93.89787,
          49.209946
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "tony",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -93.89032,
          49.21174
        ]
      }
    }
  ]
}

The layer is created with the following

 var vlayer = new OpenLayers.Layer.Vector( "Points" , {
              projection: new OpenLayers.Projection("EPSG:4326"),
              strategies: [ new OpenLayers.Strategy.Fixed(), saveStrategy],
              protocol: new OpenLayers.Protocol.HTTP({url: "./points.geojson", format: new OpenLayers.Format.GeoJSON({ignoreExtraDims: true }) })
         });

Writing

    var format = new OpenLayers.Format.GeoJSON({ projection: 'EPSG:4326', internalProjection: new OpenLayers.Projection("EPSG:4326"),  externalProjection: new OpenLayers.Projection("EPSG:4326") })

    console.log(  format.write( vlayer.features ) );

Produced GeoJSON

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "carey",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.0452912E7,
          6311114
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "dan",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.0452663E7,
          6310560
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "tony",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.0451822E7,
          6310865.5
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "dan",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.0461912E7,
          6321375.5
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "dan",
        "lure": "",
        "date": "",
        "pic": ""
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -1.0465505E7,
          6317706.5
        ]
      }
    }
  ]
}
  • Not sure how you're doing conversion between input GeoJSON and vlayer.features, using Format.GeoJSON to deserialize and serialize GeoJSON proves that it works correctly: console.log(format.write(format.read(input)));. What's printed is the same as value of input. – kryger Nov 13 '15 at 17:20
  • I added the vlayer creation in the question. Seems I should get the same projection on the write as was read. – Dan Buss Nov 13 '15 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.