Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-7222 use polylabel to calculate a point within the first polygon. #3604

Merged
merged 3 commits into from
Nov 22, 2023
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
2 changes: 2 additions & 0 deletions source/frontend/src/components/maps/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export const defaultBounds = new LatLngBounds(
[60.09114547, -119.49609429],
[48.78370426, -139.35937554],
);

export const ONE_HUNDRED_METER_PRECISION = 0.0001;
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import './PointClusterer.scss';

import { BBox, Feature, FeatureCollection, Geometry } from 'geojson';
import { BBox, Feature, FeatureCollection, Geometry, MultiPolygon, Polygon } from 'geojson';
import L, { geoJSON, LatLng } from 'leaflet';
import { find } from 'lodash';
import polylabel from 'polylabel';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { FeatureGroup, Marker, Polyline, useMap } from 'react-leaflet';
import Supercluster, { ClusterFeature, ClusterProperties, PointFeature } from 'supercluster';
Expand All @@ -18,6 +19,7 @@
PIMS_Property_Location_View,
} from '@/models/layers/pimsPropertyLocationView';

import { ONE_HUNDRED_METER_PRECISION } from '../../constants';
import SinglePropertyMarker from '../Markers/SingleMarker';
import { Spiderfier, SpiderSet } from './Spiderfier';
import { getDraftIcon, pointToLayer, zoomToCluster } from './util';
Expand Down Expand Up @@ -351,9 +353,13 @@
* @returns [lat, lng]
*/
const getLatLng = <P,>(feature: Feature<Geometry, P>) => {
if (feature.geometry.type === 'Polygon' || feature.geometry.type === 'MultiPolygon') {
const latLng = geoJSON(feature.geometry).getBounds().getCenter();
return [latLng.lng, latLng.lat];
if (feature?.geometry?.type === 'Polygon') {
return polylabel((feature.geometry as Polygon).coordinates, ONE_HUNDRED_METER_PRECISION);

Check warning on line 357 in source/frontend/src/components/maps/leaflet/Layers/PointClusterer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/components/maps/leaflet/Layers/PointClusterer.tsx#L357

Added line #L357 was not covered by tests
} else if (feature?.geometry?.type === 'MultiPolygon') {
return polylabel(

Check warning on line 359 in source/frontend/src/components/maps/leaflet/Layers/PointClusterer.tsx

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/components/maps/leaflet/Layers/PointClusterer.tsx#L359

Added line #L359 was not covered by tests
(feature.geometry as MultiPolygon).coordinates[0],
ONE_HUNDRED_METER_PRECISION,
);
} else if ('coordinates' in feature.geometry) {
// TODO: This is only needed to satisfy the types. Fix this.
const latLng = geoJSON(feature.geometry).getBounds().getCenter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ describe('MapSelectorContainer component', () => {
district: 12,
districtName: 'Cannot determine',
id: 'PID-009-727-493',
latitude: 48.7662,
latitude: 48.76613749999999,
legalDescription:
'THAT PART OF SECTION 13, RANGE 1, SOUTH SALT SPRING ISLAND, COWICHAN DISTRICT',
longitude: -123.4617,
longitude: -123.46163749999998,
name: undefined,
pid: '9727493',
pin: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ describe('PropertySelectorSearchContainer component', () => {
);
// calls the region and district layers
expect(mockAxios.history.get[1].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
expect(mockAxios.history.get[2].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
});
});
Expand All @@ -138,10 +138,10 @@ describe('PropertySelectorSearchContainer component', () => {
);
// calls the region and district layers
expect(mockAxios.history.get[1].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
expect(mockAxios.history.get[2].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
});
});
Expand All @@ -165,10 +165,10 @@ describe('PropertySelectorSearchContainer component', () => {
);
// calls the region and district layers
expect(mockAxios.history.get[1].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
expect(mockAxios.history.get[2].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
});
});
Expand Down Expand Up @@ -199,14 +199,16 @@ describe('PropertySelectorSearchContainer component', () => {

// calls the region and district layers
expect(mockAxios.history.get[1].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
expect(mockAxios.history.get[2].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);

// calls the geocoder nearest api to retrieve address
expect(mockAxios.history.get[3].url).toBe('/tools/geocoder/nearest?point=-123.4617,48.7662');
expect(mockAxios.history.get[3].url).toBe(
'/tools/geocoder/nearest?point=-123.46163749999998,48.76613749999999',
);
});
});

Expand Down Expand Up @@ -241,10 +243,10 @@ describe('PropertySelectorSearchContainer component', () => {
);
// calls the region and district layers
expect(mockAxios.history.get[3].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_REGIONAL_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
expect(mockAxios.history.get[4].url).toBe(
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.4617 48.7662))',
'https://openmaps.gov.bc.ca/geo/pub/WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.3.0&outputFormat=application/json&typeNames=pub:WHSE_ADMIN_BOUNDARIES.TADM_MOT_DISTRICT_BNDRY_POLY&srsName=EPSG:4326&count=1&cql_filter=CONTAINS(GEOMETRY,SRID=4326;POINT ( -123.46163749999998 48.76613749999999))',
);
});
});
Expand Down
12 changes: 10 additions & 2 deletions source/frontend/src/utils/mapPropertyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { toast } from 'react-toastify';

import { LocationFeatureDataset } from '@/components/common/mapFSM/useLocationFeatureLoader';
import { ONE_HUNDRED_METER_PRECISION } from '@/components/maps/constants';
import { IMapProperty } from '@/components/propertySelector/models';
import { DistrictCodes } from '@/constants/districtCodes';
import { RegionCodes } from '@/constants/regionCodes';
Expand Down Expand Up @@ -129,10 +130,17 @@
)
.map((feature): IMapProperty => {
if (feature?.geometry?.type === 'Polygon') {
const boundedCenter = polylabel((feature.geometry as Polygon).coordinates);
const boundedCenter = polylabel(
(feature.geometry as Polygon).coordinates,
ONE_HUNDRED_METER_PRECISION,
);
return toMapProperty(feature, address, boundedCenter[1], boundedCenter[0]);
} else if (feature?.geometry?.type === 'MultiPolygon') {
const boundedCenter = polylabel((feature.geometry as MultiPolygon).coordinates[0]);
const boundedCenter = polylabel(

Check warning on line 139 in source/frontend/src/utils/mapPropertyUtils.ts

View check run for this annotation

Codecov / codecov/patch

source/frontend/src/utils/mapPropertyUtils.ts#L139

Added line #L139 was not covered by tests
(feature.geometry as MultiPolygon).coordinates[0],
ONE_HUNDRED_METER_PRECISION,
);
//TODO: calculate the center of the polygon with the largest area.
return toMapProperty(feature, address, boundedCenter[1], boundedCenter[0]);
} else {
toast.error(
Expand Down
Loading