Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACRFD-13-2: Updates/tweaks to new spatial logic. #207

Merged
merged 1 commit into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions api/helpers/spatialUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var Wkx = require('wkx');
const epsg = require('epsg');
const reproject = require('reproject');

/**
* Converts interestParcels wkt geometry data into an array of ESPG:3005 data used by leaflet.
*
* @param {*} geo element of interestParcels array
* @returns [{coordinates: [], type: string}] array of ESPG:3005 objects of the form: {coordinates: [], type: string}
*/
exports.getGeometryArray = function(geo) {
if (!geo || !geo.wktGeometry) {
return [];
}

return this.convertGeoJSONToEPSG3005(this.convertWKTToGeoJson(geo.wktGeometry));
};

/**
* Converts wkt geometry data to an array of GeoJson format polygons.
*
* Note on type GeometryCollection: A GeometryCollection is the type when specifying a super-set of distinct polygons.
* But this application currently only supports polygons and stores them as separate Features (see models/features.js).
* So this function parses out the GeometryCollection sub-polygons rather than returning a single GeometryCollection
* object that has multiple sub-polygon elements.
*
* Extra: See https://geojson.org/ for GeoJson specification.
*
* @param {*} geo element of interestParcels
* @returns [{coordinates: [], type: string}] array of geoJSON objects of the form: {coordinates: [], type: string}
*/
exports.convertWKTToGeoJson = function(wktGeometry) {
if (!wktGeometry) {
return [];
}

const geoJSONArray = [];

// convert to wkt to geojson
const geoJSON = Wkx.Geometry.parse(wktGeometry).toGeoJSON();

if (geoJSON.type === 'GeometryCollection') {
// parse out GeometryCollection sub-polygons.
geoJSON.geometries.forEach(element => {
geoJSONArray.push(element);
});
} else {
geoJSONArray.push(geoJSON);
}

return geoJSONArray;
};

/**
* Converts an array of GeoJson format objects to an array of EPSG:3005 format objects usable by leaflet.
*
* @param [{coordinates: [], type: string}] array of geoJson format objects of the form: {coordinates: [], type: string}
* @returns [{coordinates: [], type: string}] array of EPSG:3005 format objects of the form: {coordinates: [], type: string}
*/
exports.convertGeoJSONToEPSG3005 = function(geoJSONArray) {
if (!geoJSONArray) {
return [];
}

const leafletFormatArray = [];

geoJSONArray.forEach(element => {
// Convert for use in leaflet coords.
leafletFormatArray.push(reproject.toWgs84(element, 'EPSG:3005', epsg));
});

return leafletFormatArray;
};
61 changes: 3 additions & 58 deletions api/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ var qs = require('qs');
var request = require('request');
var turf = require('@turf/turf');
var helpers = require('@turf/helpers');
var Wkx = require('wkx');
const epsg = require('epsg');
const reproject = require('reproject');
const defaultLog = require('winston').loggers.get('default');
const spatialUtils = require('./spatialUtils');

var _serviceHost = process.env.CLAMAV_SERVICE_HOST || '127.0.0.1';
var _servicePort = process.env.CLAMAV_SERVICE_PORT || '3310';
var _tantalisAPI = process.env.TTLS_API_ENDPOINT || 'https://api.nrs.gov.bc.ca/ttls-api/v1/';
Expand Down Expand Up @@ -328,7 +327,7 @@ exports.getApplicationByDispositionID = function(accessToken, disp) {
crs.properties = {};
crs.properties.name = 'urn:ogc:def:crs:EPSG::4326';

const geometryArray = convertToLeafletFormat(convertToGeoJson(geo));
const geometryArray = spatialUtils.getGeometryArray(geo);

geometryArray.forEach(geometry => {
application.parcels.push({
Expand Down Expand Up @@ -391,60 +390,6 @@ exports.getApplicationByDispositionID = function(accessToken, disp) {
});
};

/**
* Converts interestParcels wkt geometry data to an array of GeoJson format polygons.
*
* Note on type GeometryCollection: A GeometryCollection is the type when specifying a super-set of distinct polygons.
* But this application currently only supports polygons and stores them as separate Features (see models/features.js).
* So this function parses out the GeometryCollection sub-polygons rather than returning a single GeometryCollection
* object that has multiple sub-polygon elements.
*
* @param {*} geo element of interestParcels
* @returns [{coordinates: [], type: string}] array of geoJSON objects of the form: {coordinates: [], type: string}
*/
const convertToGeoJson = function(geo) {
if (!geo || !geo.wktGeometry) {
return [];
}

const geoJSONArray = [];

// convert to geojson
const geoJSON = Wkx.Geometry.parse(geo.wktGeometry).toGeoJSON();

if (geoJSON.type === 'GeometryCollection') {
// parse out GeometryCollection sub-polygons.
geoJSON.geometries.forEach(element => {
geoJSONArray.push(element);
});
} else {
geoJSONArray.push(geoJSON);
}

return geoJSONArray;
};

/**
* Converts an array of GeoJson format objects to an array of EPSG:3005 format objects usable by leaflet.
*
* @param [{coordinates: [], type: string}] array of geoJson format objects of the form: {coordinates: [], type: string}
* @returns [{coordinates: [], type: string}] array of EPSG:3005 format objects of the form: {coordinates: [], type: string}
*/
const convertToLeafletFormat = function(geoJSONArray) {
if (!geoJSONArray) {
return [];
}

const leafletFormatArray = [];

geoJSONArray.forEach(element => {
// Convert for use in leaflet coords.
leafletFormatArray.push(reproject.toWgs84(element, 'EPSG:3005', epsg));
});

return leafletFormatArray;
};

/**
* Fetches all application landUseApplicationIds (aka: dispositionID, tantalisID) from Tantalis given the filter params provided.
*
Expand Down