1
votes

I'm trying to parse GeoJSON data and draw 'Points' on a ESPG:4326 map, with some success. It draws the 'Point' on the map, but on the default setting [0, 0]. I am new to Openlayers. I have also put my geojson through two validators, which came back with no errors. My code below is a .js file.

I had a similar problem before when changing the 'center' of the map, where I used 'fromLonLat' to fix the issue. I assume there is some sort of solution involving 'fromLonLat', but I cannot find anything related to this anywhere on Openlayers 6.

import Feature from 'ol/Feature';
import Map from 'ol/Map';
import View from 'ol/View';
import GeoJSON from 'ol/format/GeoJSON';
import Point from 'ol/geom/Point';
import {fromLonLat} from 'ol/proj';
import {defaults as defaultInteractions, DragRotateAndZoom} from 'ol/interactio$
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';

var image = new CircleStyle({
  fill: new Fill({
        color: 'rgba(255, 0, 0, 1)'
        }),
  radius: 5,
  stroke: new Stroke({color: 'red', width: 1})
});


var markerStyle = {
  'Point': new Style({
    image: image
  })
};


var styleFunction = function(feature) {
  return markerStyle[feature.getGeometry().getType()];
};

var geojsonObject = {
   'type': 'FeatureCollection',
   'crs': {
       'type': 'name',
       'properties': {
        'name': 'ESPG:4326'
        }
  },
   'features': [
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.65, 54.34]
    },
    'properties': {
    'Site':'ARMAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.86, 54.6]
    },
   'Site':'BELFAST EAST'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.97, 54.56]
    },
    'properties': {
    'Site':'BELFAST BALMORAL'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.27, 54.86]
    },
    'properties': {
    'Site':'BALLYMENA TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.26, 54.35]
    },
    'properties': {
    'Site':'BANBRIDGE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.67, 54.65]
    },
    'properties': {
    'Site':'BANGOR REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.38, 55.09]
    },
    'properties': {
    'Site':'CREGAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point','properties': {
    'Site':'BANBRIDGE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.67, 54.65]
    },
    'properties': {
    'Site':'BANGOR REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.38, 55.09]
    },
    'properties': {
    'Site':'CREGAGH'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.66, 55.13]
    },
    'properties': {
    'Site':'COLERAINE REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.76, 54.50]
    },
    'properties': {
    'Site':'DUNGANNON'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.70, 54.32]
    },
    'properties': {
    'Site':'DOWNPATRICK TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.64, 54.34]
    },
    'properties': {
    'Site':'ENNISKILLEN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.95, 54.66]
    },
    'properties': {
    'Site':'GLENGORMLEY'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.83374, 54.59279]
    },
    'properties': {
    'Site':'BELFAST KNOCK'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.30, 54.99]
    },
    'properties': {
    'Site':'LONDONDERRY MEG'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.82, 54.85]
    },
    'properties': {
    'Site':'LARNE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.95, 55.05]
    },
    'properties': {
    'Site':'LIMAVADY'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.05, 54.51]
    },
    'properties': {
    'Site':'LISBURN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.34, 54.45]
    },
    'properties': {
    'Site':'LURGAN'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.96, 54.55]
    },
    'properties': {
    'Site':'MALONE TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.60, 54.75]
    },
    'properties': {
    'Site':'MAGHERAFELT TRANSMISSION AREA'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.70, 54.59]
    },
    'properties': {
    'Site':'NEWTOWNARDS'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-6.33, 54.17]
    },
    'properties': {
    'Site':'NEWRY TE'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-7.29, 54.60]
    },
    'properties': {
    'Site':'OMAGH SPERRIN TRUNK'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.91, 54.57]
    },
    'properties': {
    'Site':'ORMEAU REPEATER STATION'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.88, 54.61]
    },
    'properties': {
    'Site':'INTAKE'
   }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.83, 54.45]
    },
    'properties': {
    'Site':'SAINTFIELD'
    }
  },
  {
    'type': 'Feature',
    'geometry': {
       'type': 'Point',
       'coordinates':  [-5.89, 54.67]
    },
    'properties': {
    'Site':'WHITEABBEY'
    }
  }
]
}

var vectorSource = new VectorSource({
        features: new GeoJSON().readFeatures(geojsonObject, {
                dataProjection: 'ESPG:4326',
                featureProjection: 'ESPG:3857'
        })
});

var vectorLayer = new VectorLayer({
        source: vectorSource,
        style: styleFunction
});


var map = new Map({
  interactions: defaultInteractions().extend([
    new DragRotateAndZoom()
  ]),
  layers: [
    new TileLayer({
      source: new OSM()
    }),
    vectorLayer
  ],
  target: 'map',
  view: new View({
    center: fromLonLat([1.155670, 52.059311]),
    zoom: 2
  })
});



1
Your data is ESPG:4326 and the map view is ESPG:3857 so you will need features: (new GeoJSON()).readFeatures(geojsonObject, {dataProjection: 'ESPG:4326', featureProjection: 'ESPG:3857'})Mike
Mike, I tried your suggestion, but the Points are still on the 3857 default [0, 0]snewall
try with EPSG instead of ESPG (typo)JGH
Don't put [SOLVED] in the title. Add an answer and accept it (when you can) to properly close this question.Tim Lewis

1 Answers

2
votes

Your data is ESPG:4326 and the map view is ESPG:3857 so you will need features: (new GeoJSON()).readFeatures(geojsonObject, {dataProjection: 'ESPG:4326', featureProjection: 'ESPG:3857'}) – Mike 23 hours ago