diff --git a/api/prisma/seed-helpers/map-layer-factory.ts b/api/prisma/seed-helpers/map-layer-factory.ts index e571c4625a..afa4ba20db 100644 --- a/api/prisma/seed-helpers/map-layer-factory.ts +++ b/api/prisma/seed-helpers/map-layer-factory.ts @@ -22,20 +22,18 @@ export const simplifiedDCMap = { geometry: { coordinates: [ [ - [ - [-77.0392589333301, 38.79186072967565], - [-76.90981025809415, 38.89293952026222], - [-77.04122027689426, 38.996161202682146], - [-77.12000091005532, 38.93465307055658], - [-77.10561772391833, 38.91990351952725], - [-77.09123453778136, 38.90565966392609], - [-77.06802530560486, 38.9015894658674], - [-77.06181438431805, 38.889377471720564], - [-77.03697069917165, 38.870801038935525], - [-77.03043288729134, 38.850437727576235], - [-77.03435557441966, 38.80816525459605], - [-77.0392589333301, 38.79186072967565], - ], + [-77.0392589333301, 38.79186072967565], + [-76.90981025809415, 38.89293952026222], + [-77.04122027689426, 38.996161202682146], + [-77.12000091005532, 38.93465307055658], + [-77.10561772391833, 38.91990351952725], + [-77.09123453778136, 38.90565966392609], + [-77.06802530560486, 38.9015894658674], + [-77.06181438431805, 38.889377471720564], + [-77.03697069917165, 38.870801038935525], + [-77.03043288729134, 38.850437727576235], + [-77.03435557441966, 38.80816525459605], + [-77.0392589333301, 38.79186072967565], ], ], type: 'Polygon', diff --git a/api/src/services/geocoding.service.ts b/api/src/services/geocoding.service.ts index 540f363349..1e285b8a6e 100644 --- a/api/src/services/geocoding.service.ts +++ b/api/src/services/geocoding.service.ts @@ -1,4 +1,4 @@ -import { FeatureCollection, point, polygons } from '@turf/helpers'; +import { FeatureCollection, Polygon, point } from '@turf/helpers'; import buffer from '@turf/buffer'; import booleanPointInPolygon from '@turf/boolean-point-in-polygon'; import { MapLayers, Prisma } from '@prisma/client'; @@ -72,21 +72,10 @@ export class GeocodingService { Number.parseFloat(preferenceAddress.latitude.toString()), ]); - // Convert the features to the format that turfjs wants - const polygonsFromFeature = []; - featureCollectionLayers.features.forEach((feature) => { - if ( - feature.geometry.type === 'MultiPolygon' || - feature.geometry.type === 'Polygon' - ) { - feature.geometry.coordinates.forEach((coordinate) => { - polygonsFromFeature.push(coordinate); - }); - } - }); - const layer = polygons(polygonsFromFeature); - - const points = pointsWithinPolygon(preferencePoint, layer); + const points = pointsWithinPolygon( + preferencePoint, + featureCollectionLayers as FeatureCollection, + ); if (points && points.features?.length) { return true; } diff --git a/api/test/unit/services/geocoding.service.spec.ts b/api/test/unit/services/geocoding.service.spec.ts index 1fb1c59924..7c0506547d 100644 --- a/api/test/unit/services/geocoding.service.spec.ts +++ b/api/test/unit/services/geocoding.service.spec.ts @@ -6,7 +6,10 @@ import { Address } from '../../../src/dtos/addresses/address.dto'; import { ValidationMethod } from '../../../src/enums/multiselect-questions/validation-method-enum'; import { InputType } from '../../../src/enums/shared/input-type-enum'; import Listing from '../../../src/dtos/listings/listing.dto'; -import { simplifiedDCMap } from '../../../prisma/seed-helpers/map-layer-factory'; +import { + redlinedMap, + simplifiedDCMap, +} from '../../../prisma/seed-helpers/map-layer-factory'; import { FeatureCollection } from '@turf/helpers'; import { ApplicationMultiselectQuestion } from '../../../src/dtos/applications/application-multiselect-question.dto'; import { Application } from '../../../src/dtos/applications/application.dto'; @@ -29,6 +32,7 @@ describe('GeocodingService', () => { longitude: -77.0365, }; const featureCollection = simplifiedDCMap as unknown as FeatureCollection; + const featureCollection2 = redlinedMap as unknown as FeatureCollection; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -107,14 +111,25 @@ describe('GeocodingService', () => { }); it("should return 'true' if address is within layer", () => { expect(service.verifyLayers(address, featureCollection)).toBe(true); + expect( + service.verifyLayers( + { + ...address, + latitude: 37.870318963458324, + longitude: -122.30141799736678, + }, + featureCollection2, + ), + ).toBe(true); }); - it("should return 'false' if address is within layer", () => { + it("should return 'false' if address is not within layer", () => { expect( service.verifyLayers( { ...address, latitude: 39.284205, longitude: -76.621698 }, featureCollection, ), ).toBe(false); + expect(service.verifyLayers(address, featureCollection2)).toBe(false); }); });