Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- // Customise as required
- const inFile = 'geoFence.txt'
- const outFile = 'geoJSON.json'
- const geoJSON = {
- type: 'FeatureCollection',
- features: [],
- }
- fs.readFile(inFile, 'utf8', (err, data) => {
- const fences = data.match(/\[([^\]]+)\]([^\[]*)/g)
- fences.forEach(fence => {
- const geoFence = {
- type: 'Feature',
- properties: {
- name: ''
- },
- geometry: {
- type: 'Polygon',
- coordinates: [[]]
- }
- }
- geoFence.properties.name = fence.match(/\[([^\]]+)\]/)[1]
- geoFence.geometry.coordinates[0] = fence.match(/[0-9\-\.]+,\s*[0-9\-\.]+/g).map(point => [parseFloat(point.split(',')[1]), parseFloat(point.split(',')[0])])
- geoFence.geometry.coordinates[0].push(geoFence.geometry.coordinates[0][0])
- geoJSON.features.push(geoFence)
- })
- fs.writeFile(outFile, JSON.stringify(geoJSON), 'utf8', () => { })
- })
Add Comment
Please, Sign In to add comment