Skip to content
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
54 changes: 54 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"mgrs": "^2.0.0",
"node-fetch-commonjs": "^3.3.2",
"pg": "^8.11.5",
"proj4": "^2.11.0",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.8.0",
"typeorm": "^0.3.20",
"utm-latlng": "^1.0.8"
"typeorm": "^0.3.20"
},
"devDependencies": {
"@commitlint/cli": "^17.6.6",
Expand All @@ -81,6 +81,7 @@
"@types/geojson-validation": "^1.0.3",
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.7",
"@types/proj4": "^2.5.5",
"@types/supertest": "^2.0.12",
"@types/swagger-ui-express": "^4.1.3",
"commitlint": "^17.6.6",
Expand Down
3 changes: 3 additions & 0 deletions src/common/projections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const wgs84Projection = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs';

export const utmProjection = (zone: number): string => `+proj=utm +zone=${zone} +ellps=WGS84 +datum=WGS84 +units=m +no_defs`;
31 changes: 18 additions & 13 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import config from 'config';
import { estypes } from '@elastic/elasticsearch';
import utmObj from 'utm-latlng';
import proj4 from 'proj4';
import { Item } from '../item/models/item';
import { Tile } from '../tile/models/tile';
import { Route } from '../route/models/route';
import { FIELDS } from './constants';
import { utmProjection, wgs84Projection } from './projections';

export const formatResponse = <T extends Item | Tile | Route>(
elasticResponse: estypes.SearchResponse<T>
Expand Down Expand Up @@ -57,29 +58,33 @@ export const validateWGS84Coordinate = (coordinate: { lon: number; lat: number }
return true;
};

/* eslint-disable @typescript-eslint/naming-convention */
export const convertWgs84ToUTM = (
latitude: number,
longitude: number,
utmPrecision: number = 0
utmPrecision = 0
):
| string
| {
/* eslint-disable @typescript-eslint/naming-convention */
Easting: number;
Northing: number;
ZoneNumber: number;
ZoneLetter: string;
/* eslint-enable @typescript-eslint/naming-convention */
} => {
const utm = new utmObj();
//@ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
return utm.convertLatLngToUtm(latitude, longitude, utmPrecision);
const zone = Math.floor((longitude + 180) / 6) + 1;

const [easting, northing] = proj4(wgs84Projection, utmProjection(zone), [longitude, latitude]);

return {
Easting: +easting.toFixed(utmPrecision),
Northing: +northing.toFixed(utmPrecision),
ZoneNumber: zone,
};
};
/* eslint-enable @typescript-eslint/naming-convention */

export const convertUTMToWgs84 = (x: number, y: number, zone: number) => {
const utm = new utmObj();
return utm.convertUtmToLatLng(x, y, zone, 'N');
const [longitude, latitude] = proj4(utmProjection(zone), wgs84Projection, [x, y]);
return { lat: latitude, lng: longitude };
};

export const validateTile = (tile: { tileName: string; subTileNumber: number[] }): boolean => {
Expand All @@ -88,10 +93,10 @@ export const validateTile = (tile: { tileName: string; subTileNumber: number[] }
}
//regex = /^-?d+$/;
const regex = /^(\d\d)$/;
tile.subTileNumber.forEach((subTileNumber) => {
for (const subTileNumber of tile.subTileNumber) {
if (!regex.test(`${subTileNumber}`)) {
return false;
}
});
}
return true;
};
6 changes: 2 additions & 4 deletions src/latLon/controllers/latLonController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BoundCounter, Meter } from '@opentelemetry/api-metrics';
import { RequestHandler } from 'express';
import httpStatus from 'http-status-codes';
import { injectable, inject } from 'tsyringe';
import { Polygon } from 'geojson';
import { SERVICES } from '../../common/constants';
import { LatLonManager } from '../models/latLonManager';

Expand All @@ -23,10 +24,7 @@ type GetLatLonToTileHandler = RequestHandler<
type GetTileToLatLonHandler = RequestHandler<
undefined,
{
geometry: {
coordinates: number[][];
type: string;
};
geometry: Polygon;
type: string;
properties: {
TYPE: string;
Expand Down
6 changes: 2 additions & 4 deletions src/latLon/models/latLonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IConfig } from 'config';
import { Logger } from '@map-colonies/js-logger';
import { inject, injectable } from 'tsyringe';
import * as mgrs from 'mgrs';
import { Polygon } from 'geojson';
import { SERVICES } from '../../common/constants';
import { LATLON_CUSTOM_REPOSITORY_SYMBOL, LatLonRepository } from '../DAL/latLonRepository';
import { convertWgs84ToUTM, validateTile, validateWGS84Coordinate } from '../../common/utils';
Expand Down Expand Up @@ -62,10 +63,7 @@ export class LatLonManager {
}

public async tileToLatLon({ tileName, subTileNumber }: { tileName: string; subTileNumber: number[] }): Promise<{
geometry: {
coordinates: number[][];
type: string;
};
geometry: Polygon;
type: string;
properties: {
/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
8 changes: 3 additions & 5 deletions src/latLon/utlis/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Polygon } from 'geojson';
import { NotFoundError } from '../../common/errors';
import { convertUTMToWgs84 } from '../../common/utils';
import { LatLon } from '../DAL/latLon';

/* eslint-disable @typescript-eslint/naming-convention */
const geoJsonObjectTemplate = (): {
geometry: {
coordinates: number[][];
type: string;
};
geometry: Polygon;
type: string;
properties: {
TYPE: string;
Expand Down Expand Up @@ -88,7 +86,7 @@ export const getSubTileByBottomLeftUtmCoor = (
if (typeof coordiante === 'string') {
throw new Error('coordinate is string');
}
result.geometry.coordinates[0] = [coordiante.lng, coordiante.lat];
result.geometry.coordinates[0].push([coordiante.lng, coordiante.lat]);
}

result.properties.TILE_NAME = tile.tileName;
Expand Down