Skip to content

Commit

Permalink
fix: geocoding layer fix (#3951)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludtkemorgan authored Mar 19, 2024
1 parent fc912c8 commit 5c96b01
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
26 changes: 12 additions & 14 deletions api/prisma/seed-helpers/map-layer-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
21 changes: 5 additions & 16 deletions api/src/services/geocoding.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<Polygon>,
);
if (points && points.features?.length) {
return true;
}
Expand Down
19 changes: 17 additions & 2 deletions api/test/unit/services/geocoding.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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({
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 5c96b01

Please sign in to comment.