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
25 changes: 8 additions & 17 deletions src/common/elastic/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { WGS84Coordinate } from '../interfaces';

/* eslint-disable @typescript-eslint/naming-convention */
export const boundingBox = (
bbox: number[]
): {
geo_bounding_box: {
geometry: {
top_left: {
lon: number;
lat: number;
};
bottom_right: {
lon: number;
lat: number;
};
top_left: WGS84Coordinate;
bottom_right: WGS84Coordinate;
};
};
} => ({
Expand All @@ -29,17 +25,12 @@ export const boundingBox = (
},
});

export const geoDistance = (params: {
radius: number;
lon: number;
lat: number;
}): {
export const geoDistance = (
params: WGS84Coordinate & { radius: number }
): {
geo_distance: {
distance: string;
geometry: {
lon: number;
lat: number;
};
geometry: WGS84Coordinate;
};
} => ({
geo_distance: {
Expand Down
5 changes: 5 additions & 0 deletions src/common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export interface Geometry {
export interface FeatureCollection<T extends Feature> extends GeoJSONFeatureCollection {
features: T[];
}

export interface WGS84Coordinate {
lat: number;
lon: number;
}
13 changes: 3 additions & 10 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Tile } from '../tile/models/tile';
import { Route } from '../route/models/route';
import { FIELDS } from './constants';
import { utmProjection, wgs84Projection } from './projections';
import { FeatureCollection } from './interfaces';
import { FeatureCollection, WGS84Coordinate } from './interfaces';

export const formatResponse = <T extends Item | Tile | Route>(elasticResponse: estypes.SearchResponse<T>): FeatureCollection<T> => ({
type: 'FeatureCollection',
Expand Down Expand Up @@ -78,16 +78,9 @@ export const convertWgs84ToUTM = (
};
/* eslint-enable @typescript-eslint/naming-convention */

export const convertUTMToWgs84 = (
x: number,
y: number,
zone: number
): {
lat: number;
lng: number;
} => {
export const convertUTMToWgs84 = (x: number, y: number, zone: number): WGS84Coordinate => {
const [longitude, latitude] = proj4(utmProjection(zone), wgs84Projection, [x, y]);
return { lat: latitude, lng: longitude };
return { lat: latitude, lon: longitude };
};

export const validateTile = (tile: { tileName: string; subTileNumber: number[] }): boolean => {
Expand Down
14 changes: 3 additions & 11 deletions src/item/controllers/itemController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ import { injectable, inject } from 'tsyringe';
import { SERVICES } from '../../common/constants';
import { ItemManager } from '../models/itemManager';
import { Item } from '../models/item';
import { GeoContext } from '../../common/interfaces';
import { FeatureCollection, GeoContext } from '../../common/interfaces';

type GetResourceHandler = RequestHandler<
undefined,
{
type: string;
features: (Item | undefined)[];
},
undefined,
GetItemsQueryParams
>;
type GetItemsHandler = RequestHandler<undefined, FeatureCollection<Item>, undefined, GetItemsQueryParams>;

export interface GetItemsQueryParams {
command_name: string;
Expand All @@ -40,7 +32,7 @@ export class ItemController {
this.createdResourceCounter = meter.createCounter('created_resource');
}

public getItems: GetResourceHandler = async (req, res, next) => {
public getItems: GetItemsHandler = async (req, res, next) => {
try {
const { command_name: commandName, tile, sub_tile, geo_context, reduce_fuzzy_match, size } = req.query;
const response = await this.manager.getItems(
Expand Down
1 change: 1 addition & 0 deletions src/latLon/DAL/latLonRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { FactoryFunction } from 'tsyringe';
import { LatLon as LatLonDb } from './latLon';

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const createLatLonRepository = (dataSource: DataSource) => {
return dataSource.getRepository(LatLonDb).extend({
async latLonToTile({ x, y, zone }: { x: number; y: number; zone: number }): Promise<LatLonDb | null> {
Expand Down
21 changes: 4 additions & 17 deletions src/latLon/controllers/latLonController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { injectable, inject } from 'tsyringe';
import { SERVICES } from '../../common/constants';
import { LatLonManager } from '../models/latLonManager';
import { Tile } from '../../tile/models/tile';
import { FeatureCollection } from '../../common/interfaces';
import { FeatureCollection, WGS84Coordinate } from '../../common/interfaces';

type GetLatLonToTileHandler = RequestHandler<
undefined,
Expand All @@ -23,29 +23,16 @@ type GetTileToLatLonHandler = RequestHandler<undefined, FeatureCollection<Tile>,

type GetLatLonToMgrsHandler = RequestHandler<undefined, { mgrs: string }, undefined, GetLatLonToMgrsQueryParams>;

type GetMgrsToLatLonHandler = RequestHandler<
undefined,
{
lat: number;
lon: number;
},
undefined,
GetMgrsToLatLonQueryParams
>;
type GetMgrsToLatLonHandler = RequestHandler<undefined, WGS84Coordinate, undefined, GetMgrsToLatLonQueryParams>;

export interface GetLatLonToTileQueryParams {
lat: number;
lon: number;
}
export interface GetLatLonToTileQueryParams extends WGS84Coordinate {}

export interface GetTileToLatLonQueryParams {
tile: string;
sub_tile_number: number[];
}

export interface GetLatLonToMgrsQueryParams {
lat: number;
lon: number;
export interface GetLatLonToMgrsQueryParams extends WGS84Coordinate {
accuracy?: number;
}
export interface GetMgrsToLatLonQueryParams {
Expand Down
4 changes: 2 additions & 2 deletions src/latLon/models/latLonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { convertWgs84ToUTM, validateTile, validateWGS84Coordinate } from '../../
import { convertTilesToUTM, getSubTileByBottomLeftUtmCoor, validateResult } from '../utlis';
import { BadRequestError } from '../../common/errors';
import { Tile } from '../../tile/models/tile';
import { FeatureCollection } from '../../common/interfaces';
import { FeatureCollection, WGS84Coordinate } from '../../common/interfaces';

@injectable()
export class LatLonManager {
Expand All @@ -22,7 +22,7 @@ export class LatLonManager {
this.dbSchema = this.config.get('db.postgresql.schema');
}

public async latLonToTile({ lat, lon }: { lat: number; lon: number }): Promise<{
public async latLonToTile({ lat, lon }: WGS84Coordinate): Promise<{
tileName: string;
subTileNumber: number[];
}> {
Expand Down
2 changes: 1 addition & 1 deletion src/latLon/utlis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const getSubTileByBottomLeftUtmCoor = (
if (typeof coordiante === 'string') {
throw new Error('coordinate is string');
}
polygon.coordinates[0].push([coordiante.lng, coordiante.lat]);
polygon.coordinates[0].push([coordiante.lon, coordiante.lat]);
}

result.features[0].geometry = polygon;
Expand Down
15 changes: 2 additions & 13 deletions src/route/DAL/queries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { estypes } from '@elastic/elasticsearch';
import { boundingBox, geoDistance } from '../../common/elastic/utils';
import { Route } from '../models/route';
import { GeoContext } from '../../common/interfaces';
import { GeoContext, WGS84Coordinate } from '../../common/interfaces';

export interface RouteQueryParams {
commandName: string;
Expand Down Expand Up @@ -61,17 +60,7 @@ export const queryForControlPointInRoute = (params: RouteQueryParams & Required<
},
},
...(params.geo?.bbox ? [boundingBox(params.geo.bbox)] : []),
...(params.geo?.radius
? [
geoDistance(
params.geo as {
radius: number;
lon: number;
lat: number;
}
),
]
: []),
...(params.geo?.radius ?? 0 ? [geoDistance(params.geo as WGS84Coordinate & { radius: number })] : []),
],
filter: [
{
Expand Down
15 changes: 3 additions & 12 deletions src/route/controllers/routeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ import { RequestHandler } from 'express';
import httpStatus from 'http-status-codes';
import { injectable, inject } from 'tsyringe';
import { SERVICES } from '../../common/constants';

import { RouteManager } from '../models/routeManager';
import { Route } from '../models/route';
import { GeoContext } from '../../common/interfaces';
import { FeatureCollection, GeoContext } from '../../common/interfaces';

type GetResourceHandler = RequestHandler<
undefined,
{
type: string;
features: (Route | undefined)[];
},
undefined,
GetRoutesQueryParams
>;
type GetRoutesHandler = RequestHandler<undefined, FeatureCollection<Route>, undefined, GetRoutesQueryParams>;

export interface GetRoutesQueryParams {
command_name: string;
Expand All @@ -40,7 +31,7 @@ export class RouteController {
this.createdResourceCounter = meter.createCounter('created_resource');
}

public getRoutes: GetResourceHandler = async (req, res, next) => {
public getRoutes: GetRoutesHandler = async (req, res, next) => {
try {
const { command_name: commandName, control_point, geo_context, reduce_fuzzy_match, size } = req.query;
const response = await this.manager.getRoutes(
Expand Down
11 changes: 4 additions & 7 deletions src/tile/controllers/tileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { RequestHandler } from 'express';
import httpStatus from 'http-status-codes';
import { injectable, inject } from 'tsyringe';
import { SERVICES } from '../../common/constants';

import { TileManager } from '../models/tileManager';
import { Tile } from '../models/tile';
import { FeatureCollection } from '../../common/interfaces';

type GetResourceHandler = RequestHandler<
type GetTilesHandler = RequestHandler<
undefined,
| {
type: string;
features: (Tile | undefined)[];
}
| FeatureCollection<Tile>
| {
type: string;
message: string;
Expand Down Expand Up @@ -42,7 +39,7 @@ export class TileController {
this.createdResourceCounter = meter.createCounter('created_resource');
}

public getTiles: GetResourceHandler = async (req, res, next) => {
public getTiles: GetTilesHandler = async (req, res, next) => {
try {
const { tile, sub_tile, reduce_fuzzy_match, size } = req.query;
const response = await this.manager.getTiles(
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/common/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../../src/common/utils';
import config from '../../../config/test.json';
import { FIELDS } from '../../../src/common/constants';
import { WGS84Coordinate } from '../../../src/common/interfaces';
import {
itemElasticResponse,
itemExpectedFormattedResponse,
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('utils', () => {

it('should convert UTM to WGS84', () => {
const result = convertUTMToWgs84(630048, 4330433, 29);
expect(result).toMatchObject({ lat: 39.11335578352079, lng: -7.495780486809503 });
expect(result).toMatchObject({ lat: 39.11335578352079, lon: -7.495780486809503 });
});

it('should convert WGS84 to UTM', () => {
Expand All @@ -60,7 +61,7 @@ describe('utils', () => {
expect(validateTile(tile as never)).toBe(expected);
});

test.each([
test.each<[WGS84Coordinate, boolean]>([
[{ lon: 50, lat: 50 }, true],
[{ lon: 190, lat: 50 }, false],
[{ lon: 50, lat: 190 }, false],
Expand Down