<commit_before>const proj = require('../../../services/proj');
const status = require('http-status-codes');

module.exports = function (req, res) {
  // Parse and detect coordinates from the given string in the given system.
  //
  const systemName = req.body.coordinateSystem;
  const text = req.body.coordinatesText;

  if (!systemName) {
    // TODO check if system exists
    return res.status(status.BAD_REQUEST).send('Invalid coordinate system');
  }

  if (!text || typeof text !== 'string') {
    return res.status(status.BAD_REQUEST).send('Missing coordinate string');
  }

  // TODO parse
  // TODO convert to WGS84 if not in WGS84

  return res.json({
    coordinates: [
      {
        system: systemName,
        xy: [12, 34],
        pretty: 'foobar',
      },
      {
        system: 'WGS84',
        xy: [56, 78],
        pretty: 'foobar',
      },
    ],
  });
};<commit_msg>PoC coordinate parser service<commit_after>const proj = require('../../../services/proj');
const status = require('http-status-codes');
const utmParser = require('utm-coordinate-parser');
const CoordinateParser = require('coordinate-parser');

module.exports = function (req, res) {
  // Parse and detect coordinates from the given string in the given system.
  //
  const systemName = req.body.coordinateSystem;
  const text = req.body.coordinatesText;

  if (!systemName || !proj.hasCoordinateSystem(systemName)) {
    const msg = 'Invalid coordinate system: ' + systemName;
    return res.status(status.BAD_REQUEST).send(msg);
  }

  if (!text || typeof text !== 'string') {
    return res.status(status.BAD_REQUEST).send('Missing coordinate string');
  }

  const projDef = proj.getProjectionDefinition(systemName);
  const projection = proj.parseProjectionDefinition(projDef);

  // Parse the coordinate text according to projection type.
  let xy;
  if (projection.projName === 'longlat') {
    // Use 3rd party parser
    const position = new CoordinateParser(text);
    xy = {
      x: position.getLongitude(),
      y: position.getLatitude(),
    };
  } else if (projection.projName === 'utm') {
    // Use utm parser
    const ne = utmParser(text);
    xy = {
      x: ne.x,
      y: ne.y,
    };
  }

  // Convert to WGS84 if not in WGS84
  const wgs84xy = proj.projectFrom(projection, xy);

  return res.json({
    coordinates: [
      {
        system: systemName,
        xy: xy,
      },
      {
        system: 'WGS84',
        xy: wgs84xy,
      },
    ],
  });
};
{'fraction_non_alphanumeric': 0.13950667205822886, 'fraction_numerical': 0.010109179134654266, 'mean_word_length': 2.7598784194528876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257010', 'n_tokens_mistral': 794, 'n_tokens_neox': 752, 'n_words': 201}