daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Jan 18th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs')
  2.  
  3. // Customise as required
  4. const inFile = 'geoFence.txt'
  5. const outFile = 'geoJSON.json'
  6.  
  7. const geoJSON = {
  8.   type: 'FeatureCollection',
  9.   features: [],
  10. }
  11.  
  12. fs.readFile(inFile, 'utf8', (err, data) => {
  13.   const fences = data.match(/\[([^\]]+)\]([^\[]*)/g)
  14.   fences.forEach(fence => {
  15.     const geoFence = {
  16.       type: 'Feature',
  17.       properties: {
  18.         name: ''
  19.       },
  20.       geometry: {
  21.         type: 'Polygon',
  22.         coordinates: [[]]
  23.       }
  24.     }
  25.     geoFence.properties.name = fence.match(/\[([^\]]+)\]/)[1]
  26.     geoFence.geometry.coordinates[0] = fence.match(/[0-9\-\.]+,\s*[0-9\-\.]+/g).map(point => [parseFloat(point.split(',')[1]), parseFloat(point.split(',')[0])])
  27.     geoFence.geometry.coordinates[0].push(geoFence.geometry.coordinates[0][0])
  28.  
  29.     geoJSON.features.push(geoFence)
  30.   })
  31.  
  32.   fs.writeFile(outFile, JSON.stringify(geoJSON), 'utf8', () => { })
  33. })
RAW Paste Data
Top