Source: tools/services/geojsons.js

import GeoJSON from 'ol/format/GeoJSON'

import isObject from 'lodash/isObject'

/**
 * Permet de convertir un geoJson en ol.Feature
 *
 * @param {ArrayBuffer | Document | Element | Object | string} geoJson source geoJSON
 * @param {string | ol.ProjectionLike} originProjection Système de projection du geoJson. Par défaut, geoJson.crs.properties.name
 * @param {string | ol.ProjectionLike} destProjection Système de projection des features retournées
 * @return {Array<ol.Feature>}                  Features converties
 */
export function geoJsonToFeature (geoJson, originProjection, destProjection) {
  let dataProj = originProjection

  if (!dataProj && isObject(geoJson) && geoJson.crs && geoJson.crs.properties && geoJson.crs.properties.name &&
    geoJson.crs.properties.name.search(/^EPSG:[0-9]*$/) === 0) {
    dataProj = geoJson.crs.properties.name
  }

  if (!destProjection) {
    destProjection = dataProj
  }

  return (new GeoJSON()).readFeatures(geoJson, (dataProj === destProjection
    ? {}
    : {
        dataProjection: dataProj,
        featureProjection: destProjection,
      }))
}

/**
 * Permet de convertir des feature en chaine de caractères
 *
 * @param  {Array<ol.Feature>} features         Features à convertir
 * @param  {string}            originProjection Système de projection des données a convertir
 * @param  {string}            destProjection   Système de projection des features encodée
 * @return {string}            Features converties
 */
export function featureToGeoJson (features, originProjection, destProjection) {
  // let featuresConvert = []

  const dataProj = originProjection
  if (!destProjection) {
    destProjection = dataProj
  }
  // Si nécessite une convertion
  /* if (originProjection !== destProjection) {
    features.forEach(feature => {
      const clone = feature.clone()
      const coord = convertCoordinates(clone.getGeometry().getCoordinates(), originProjection, destProjection)
      clone.getGeometry().setCoordinates(coord)
      featuresConvert.push(clone)
    })
  } else {
    featuresConvert = features
  } */

  // DHE, on utilise plutot la conversion effectuée par le formater
  // j'ai laissé l'ancien code au cas ou je me sois trompé de "sens"
  return (new GeoJSON()).writeFeatures(features, (dataProj === destProjection
    ? {}
    : {
        dataProjection: destProjection,
        featureProjection: dataProj,
      }))
}

/**
 * Permet de convertir des feature en objet
 *
 * @param  {Array<ol.Feature>} features         Features à convertir
 * @param  {string}            originProjection Système de projection des données a convertir
 * @param  {string}            destProjection   Système de projection des features encodée
 * @return {string}            Features converties
 */
export function featureToGeoJsonObject (features, originProjection, destProjection) {
  // let featuresConvert = []

  const dataProj = originProjection
  if (!destProjection) {
    destProjection = dataProj
  }
  // j'ai laissé l'ancien code au cas ou je me sois trompé de "sens"
  return (new GeoJSON()).writeFeaturesObject(features, (dataProj === destProjection
    ? {}
    : {
        dataProjection: destProjection,
        featureProjection: dataProj,
      }))
}

/**
 * Permet de convertir un geoJson en ol.geom
 *
 * @param {ArrayBuffer | Document | Element | Object | string} geoJson source geoJSON
 * @param {string | ol.ProjectionLike} originProjection Système de projection du geoJson. Par défaut, geoJson.crs.properties.name
 * @param {string | ol.ProjectionLike} destProjection Système de projection des features retournées
 * @return {Array<ol.geom>}                  Features converties
 */
export function geoJsonToGeometry (geoJson, originProjection, destProjection) {
  const dataProj = originProjection

  if (!destProjection) {
    destProjection = dataProj
  }

  return (new GeoJSON()).readGeometry(geoJson, (dataProj === destProjection
    ? {}
    : {
        dataProjection: dataProj,
        featureProjection: destProjection,
      }))
}