Skip to content

Commit

Permalink
feat: coordinates geo projection legale convert (#62)
Browse files Browse the repository at this point in the history
* feat: coordinates geo projection legale convert

* fix: localisation condition

* fix: refactor localisation

* fix: refactor localisation
  • Loading branch information
abelkhay authored May 2, 2023
1 parent a68d084 commit 14a1d00
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"dotenv": "^16.0.3",
"flat": "^5.0.2",
"inquirer": "^8.0.0",
"proj4": "^2.9.0",
"typescript": "^4.8.4"
}
}
21 changes: 18 additions & 3 deletions src/transformer/fields/localisation/localisation.field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ describe('localisation field', (): void => {
);
});

it('should return null when coordinates is not validate', (): void => {
it('should convert coordindates geographique projection legal to validate coordinates', (): void => {
const source: DataSource = {
bf_latitude: '6789183.34',
bf_longitude: '0021760109.06045345'
bf_longitude: '352113.49'
};
const localisation: Localisation = processLocalisation(source, STANDARD_MATCHING);

expect(localisation).toStrictEqual<Localisation>(NO_LOCALISATION);
expect(localisation).toStrictEqual<Localisation>(
Localisation({
latitude: 48.1102680182028,
longitude: -1.6770949916427347
})
);
});

it('should return null when there is no latitude or longitude colonnes', (): void => {
Expand All @@ -126,4 +131,14 @@ describe('localisation field', (): void => {

expect(localisation).toStrictEqual<Localisation>(NO_LOCALISATION);
});

it('should return null when its not validate coordinates', (): void => {
const source: DataSource = {
bf_latitude: '8.8888888.8',
bf_longitude: '9.99999999.999'
};
const localisation: Localisation = processLocalisation(source, STANDARD_MATCHING);

expect(localisation).toStrictEqual<Localisation>(NO_LOCALISATION);
});
});
16 changes: 15 additions & 1 deletion src/transformer/fields/localisation/localisation.field.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Localisation, LocalisationToValidate, isValidLocalisation } from '@gouvfr-anct/lieux-de-mediation-numerique';
import { Colonne, Dissociation, LieuxMediationNumeriqueMatching, DataSource } from '../../input';

/* eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/typedef, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
const proj4 = require('proj4');

type NoLocalisation = { noLocalisation: true } & null;
export const NO_LOCALISATION: Localisation = null as NoLocalisation;

Expand All @@ -16,11 +19,22 @@ const dissocier = (source: DataSource, localisation: Dissociation & Partial<Colo
?.split(localisation.dissocier.séparateur)
.filter((coord: string): boolean => coord !== '')[localisation.dissocier.partie];

const checkFormatLocalisation = (localisation: LocalisationToValidate): Localisation => {
if (localisation === NO_LOCALISATION || Number.isNaN(localisation.latitude) || Number.isNaN(localisation.longitude))
return NO_LOCALISATION;
proj4.defs(
'EPSG:9793',
'+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs'
);
const [longitude, latitude]: number[] = proj4('EPSG:9793', 'EPSG:4326', [localisation.longitude, localisation.latitude]);
return latitude != null && longitude != null ? Localisation({ latitude, longitude }) : NO_LOCALISATION;
};

const localisationField = (source: DataSource, localisation: Dissociation & Partial<Colonne>): string | undefined =>
(isColonne(localisation) ? source[localisation.colonne] : dissocier(source, localisation))?.toString().replace(',', '.');

const validateLocalisationField = (localisationToValidate: LocalisationToValidate): Localisation =>
isValidLocalisation(localisationToValidate) ? localisationToValidate : NO_LOCALISATION;
isValidLocalisation(localisationToValidate) ? localisationToValidate : checkFormatLocalisation(localisationToValidate);

const localisationFromMatching = (source: DataSource, matching: LieuxMediationNumeriqueMatching): LocalisationToValidate => ({
latitude: +(localisationField(source, matching.latitude) ?? NO_LOCALISATION_COLONNE),
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3453,6 +3453,11 @@ merge2@^1.3.0, merge2@^1.4.1:
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/mgrs/-/mgrs-1.0.0.tgz#fb91588e78c90025672395cb40b25f7cd6ad1829"
integrity sha512-awNbTOqCxK1DBGjalK3xqWIstBZgN6fxsMSiXLs9/spqWkF2pAhb2rrYCFSsr1/tT7PhcDGjZndG8SWYn0byYA==

micromatch@^4.0.4, micromatch@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.5.tgz#bc8999a7cbbf77cdc89f132f6e467051b49090c6"
Expand Down Expand Up @@ -3844,6 +3849,14 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

proj4@^2.9.0:
version "2.9.0"
resolved "https://registry.yarnpkg.com/proj4/-/proj4-2.9.0.tgz#1b61db8c998313f80e2514609b339fb2d28cbb82"
integrity sha512-BoDXEzCVnRJVZoOKA0QHTFtYoE8lUxtX1jST38DJ8U+v1ixY70Kpwi0Llu6YqSWEH2xqu4XMEBNGcgeRIEywoA==
dependencies:
mgrs "1.0.0"
wkt-parser "^1.3.1"

prompts@^2.0.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
Expand Down Expand Up @@ -4610,6 +4623,11 @@ which@^2.0.1:
dependencies:
isexe "^2.0.0"

wkt-parser@^1.3.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/wkt-parser/-/wkt-parser-1.3.2.tgz#deeff04a21edc5b170a60da418e9ed1d1ab0e219"
integrity sha512-A26BOOo7sHAagyxG7iuRhnKMO7Q3mEOiOT4oGUmohtN/Li5wameeU4S6f8vWw6NADTVKljBs8bzA8JPQgSEMVQ==

word-wrap@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
Expand Down

0 comments on commit 14a1d00

Please sign in to comment.