0

The coords variable has been tested with 3857 and 3067 data. Using the below code, I understand it should expect 3067 and output as 3857.

However, 3857 code coordinates work fine (and I assume they shouldn't), but the 3067 ones don't show up at all. I logged the source var below and projection_ is null. Not sure is thats the problem and how to solve it.

Final goal for this part of code is: read coordinates from DB (format 3067) then output as map on a 3857 map

  var coords=this.$store.state.MapsSingle.item.coordinates
    var geojsonObject = {

    'type': 'Feature',
    'geometry': {
    'type': 'Polygon',
    'coordinates': [JSON.parse(coords)]
  }
              };
var source = new VectorSource({features: (new GeoJSON({
    dataProjection: 'EPSG:3067',
    featureProjection: 'EPSG:3857'
})).readFeatures(geojsonObject)});
  • It looks like my json might be relevant to the question. The JSON object works in 3857, this is the 3067 version [[339669.921,6683816.682],[339706.477,6683898.422],[339745.442,6683860.681],[339726.609,6683839.079],[339715.790,6683829.431],[ 339715.415,6683829.096],[339709.086,6683823.453],[339669.921,6683816.682]] – TDawg Sep 26 '18 at 16:01
  • I tried also the solution here, it seems close to my issue, no effect gis.stackexchange.com/questions/244285/… – TDawg Sep 26 '18 at 16:09
  • bear in mind that latest GeoJSON only has ONE projection (CRS:84) – nmtoken Sep 26 '18 at 18:02
  • @nmtoken that was the key for me. I ended up trying another approach based on that. var tmpCoord3857=proj4.default.transform(source, dest, tmpCoord); I convert each row of the object to 3857 (in the future I will do this before saving to DB to save the processing time), ince the object is 3857 I can use my existing code – TDawg Sep 26 '18 at 18:43
  • I've read through the OL4 documentation and it seems the data itself can override the dataProjection set in the format (probably why it was called defaultDataProjection in OL4) which might explain why EPSG:3857 worked with 3067 set. If the option is set in readFeatures (where it was dataProjection even in OL4) that will override anything OpenLayers thinks the data is. – Mike Sep 26 '18 at 20:33
1

I want to thank @mike and @nmtoken for all the effort helping me today.

I ended up converting the object to 3857. My current code still needs to be cleaned up, but it works..

proj4.default.defs("EPSG:3067", "+proj=utm +zone=35 +ellps=GRS80 +units=m +no_defs");
var source = new proj4.default.Proj('EPSG:3067');    
var dest = new proj4.default.Proj('EPSG:3857');
var coord3857 = [];
var coords2=JSON.parse(this.$store.state.MapsSingle.item.coordinates)
for (var key in coords2) {
    if (coords2.hasOwnProperty(key)) {
        var tmpCoord=new proj4.default.toPoint(coords2[key]);
        var tmpCoord3857=proj4.default.transform(source, dest, tmpCoord);
        coord3857.push([tmpCoord3857.x,tmpCoord3857.y]);
    }
}
  • Glad you found an easy way to work round the problem. I've just added the significant parts of the original code used in my example to the answer, if it works with 3857 coordinates for you and 4326 coordinates for me it should work with any projection – Mike Sep 26 '18 at 19:12
0

try

var source = new VectorSource({format: (new GeoJSON({
    featureProjection: 'EPSG:3067'
})).readFeatures(geojsonObject, { featureProjection: map.getView().getProjection() })});

Here is the full processing I based that on, minus some error handling. Written in OL4 sysntax it's part of a timer function which reads the json url and style requirements from the head of a queue. The data is in LonLat format abd returned by the xhr as a geojson. The function has been successfully used with EPSG:3857, EPSG:27700 and EPSG:2154 views. Maybe both featureProjection and defaultDataProjection/dataProjection (it's different in OL4 and OL5) need to be specified in the format if not the dataProjection default of EPSG:4326, however the view project needs to be an option in readFeatures. I also think it's significant that EPSG:3857 data worked with featureProjection set to EPSG:3857 despite dataProjection being different.

      var url = vectorQueue[0].url;

      var tVector = new ol.layer.Vector({
          source: new ol.source.Vector({ format: new ol.format.GeoJSON({ featureProjection: hEpsg4326 }),
          style: new ol.style.Style({
              stroke: new ol.style.Stroke({
                        color: vectorQueue[0].strokeColor,
                        lineDash: vectorQueue[0].strokeDashstyle,
                        width: vectorQueue[0].strokeWidth
              })
          }),
      });

      function loadResponse(responseText) {
          var vectorSource = tVector.getSource();
          var features = vectorSource.getFormat().readFeatures(
              responseText,
              { featureProjection: iMyMap.getView().getProjection() }
          );
          vectorSource.addFeatures(features);
      }

      var xhr = new XMLHttpRequest();

      xhr.onreadystatechange = function() {
          if (xhr.readyState == 4) {
              if (xhr.status == 200) {
                  loadResponse(xhr.responseText);
              }
          }
      }

      xhr.open("GET", url, true);
      xhr.send();

      vectorQueue.splice(0,1);
      iMyMap.addLayer(tVector);
  • for me, map variable is defined later, I tried your above, but with EPSG:3857 in the place of getProjection, I hope its the same, but it has no effect. If I need to move the declaration of map, I can try too – TDawg Sep 26 '18 at 16:21
  • 'EPSG:3857' would be enough in your case, I copied the example from a script designed to be used whatever the map projection. – Mike Sep 26 '18 at 16:26
  • Thanks @mike, but it seems nothing. I should be seeing a polygon with a corner here epsg.io/… but nothing – TDawg Sep 26 '18 at 16:34
  • My example had used "format" instead of "features" but I didn't notice your code was different. Changed it now, but also in mine the declaration of source and the read source.getFormat().readFeatures(..) are separate (due to how the data is obtained) which might be significant – Mike Sep 26 '18 at 16:53
  • just to be sure, I copy/pasted your code, and commented out mine (only changing to EPSG:3857) still no change. I tried both format and features – TDawg Sep 26 '18 at 17:12

Your Answer

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

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