Converted coords to WGS84 degrees
Posted by mholmes on 29 Nov 2017 in Activity log
I've solved the problem of converting the coordinates from 32610 in metres to WGS84 in the process of prepping the data for insertion into the MySQL dbs. This is the script:
/* This file is designed to be run against a series of GeoJSON * files to create SQL statements which insert polygon and * area information into our Land Title Databases. */ /* The GeoJSON files are generated from QGIS. */ // We need fileio and proj4 for coordinate conversion. var fs = require("fs"), content, i, maxi, j, maxj, k, maxk, l, maxl, gj, output, id, area, pathBits, outFile, arrCoords, degreeCoords, coords, ids=[], idList='', idCheck='', vancouverFiles=[], mapleRidgeFiles=[], proj4 = require("proj4"); //We create these Proj4JS definitions so we can more easily refer to them later. proj4.defs('epsg32610', '+proj=utm +zone=10 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'); proj4.defs('WGS84', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees"); //GeoJSON files //Two sets of data are for properties in the Vancouver db. vancouverFiles.push('KitsilanoPlans/MergedKitsilano/MergedKitsilano_geom.json'); vancouverFiles.push('PowellStreetPlans/MergedPowellStreet/MergedPowell_geom.json'); //Three sets are for properties in the Maple Ridge db. mapleRidgeFiles.push('MapleRidgePlans/MergedMapleRidge/MergedMapleRidge_geom.json'); mapleRidgeFiles.push('SaltSpringPlans/MergedSaltSpring/MergedSaltSpring_geom.json'); mapleRidgeFiles.push('StevestonPlans/MergedSteveston/MergedSteveston_geom.json'); console.log("\n *START* \n"); processFileArray(vancouverFiles, 'vancouver.sql'); processFileArray(mapleRidgeFiles, 'mapleRidge.sql'); //Process an array of files, creating a single output file //with SQL commands to set the coordinate and area fields. function processFileArray(files, outputFile){ output = ''; for (i=0, maxi=files.length; i<maxi; i++){ ids = []; content = fs.readFileSync(files[i]); console.log('Read in ' + files[i] + '.'); gj = JSON.parse(content); console.log('Found ' + gj.features.length + ' features in the file.') for (j=0, maxj=gj.features.length; j<maxj; j++){ //The UID is the value of the prp_property_id field in the database props table. id = gj.features[j].properties["UID"]; if (id !== null){ //Keep a list of ids so we can do some checking later if necessary. ids.push('"' + id + '"'); //The area in sqm needs to be rounded to two decimal places. area = Math.round(parseFloat(gj.features[j].properties.area) * 100) / 100; //Here we convert the original coordinates from 32610/metres to WGS84 (for GeoJSON). arrCoords = gj.features[j].geometry.coordinates; for (k=0, maxk=arrCoords.length; k<maxk; k++){ for (l=0, maxl=arrCoords[k].length; l<maxl; l++){ degreeCoords = proj4(proj4('epsg32610'), proj4('WGS84'), arrCoords[k][l]); //console.log(JSON.stringify(arrCoords[k][l])); //console.log(JSON.stringify(degreeCoords)); arrCoords[k][l] = degreeCoords; } } coords = JSON.stringify(gj.features[j].geometry.coordinates); //Write the two update statements for this property record. output += 'UPDATE `props` set `prp_coords` = "' + coords + '" WHERE `prp_property_id`="' + id + '";\n'; output += 'UPDATE `props` set `prp_area` = "' + area + '" WHERE `prp_property_id`="' + id + '";\n'; } } idList = '(' + ids.join(', ') + ')'; idCheck = '#id check: \n\n# ' + 'let $ids := ' + idList + ' return for $i in $ids where not(//prp_property_id[. = $i]) return $i \n\n' //idCheck = 'CREATE TYPE id_nums AS TABLE OF NUMBER;\n'; //idCheck += 'SELECT * FROM TABLE(id_nums(' + idList + ')) ' //idCheck += ' WHERE NOT IN (SELECT `prp_property_id` from `props`)\n\n'; output = idCheck + output; } fs.writeFile(outputFile, output, function(err){}); } console.log("\n *EXIT* \n");
This entry was posted by Martin and filed under Activity log.