Skip to content

Commit

Permalink
place-converter change (#29)
Browse files Browse the repository at this point in the history
- Change GeoJSON property nesting
  • Loading branch information
dayjournal authored Oct 10, 2023
1 parent 44f6a16 commit 44b0292
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 46 deletions.
209 changes: 206 additions & 3 deletions src/to-geojson/place-converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FeatureCollection } from "geojson";
import { emptyFeatureCollection } from "./utils";

describe("placeToFeatureCollection", () => {
it("should convert GetPlaceResponse to a FeatureCollection with a single feature", () => {
it("should convert GetPlaceResponse to a FeatureCollection with a single feature and nested properties when flattenProperties is false or undefined", () => {
const input: GetPlaceResponse = {
Place: {
Label: "Test Place",
Expand Down Expand Up @@ -56,7 +56,50 @@ describe("placeToFeatureCollection", () => {
expect(placeToFeatureCollection(input)).toEqual(output);
});

it("should convert SearchPlaceIndexForTextResponse to a FeatureCollection with a multiple features", () => {
it("should convert GetPlaceResponse to a FeatureCollection with a single feature and flattened properties when flattenProperties is true", () => {
const input: GetPlaceResponse = {
Place: {
Label: "Test Place",
Geometry: {
Point: [1, 2],
},
AddressNumber: "111",
Street: "Burrard St",
Neighborhood: "Downtown",
Municipality: "Vancouver",
SubRegion: "Metro Vancouver",
Region: "British Columbia",
Country: "CAN",
PostalCode: "V6C",
},
};
const output: FeatureCollection = {
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {
"Place.AddressNumber": "111",
"Place.Country": "CAN",
"Place.Label": "Test Place",
"Place.Municipality": "Vancouver",
"Place.Neighborhood": "Downtown",
"Place.PostalCode": "V6C",
"Place.Region": "British Columbia",
"Place.Street": "Burrard St",
"Place.SubRegion": "Metro Vancouver",
},
geometry: {
type: "Point",
coordinates: [1, 2],
},
},
],
};
expect(placeToFeatureCollection(input, { flattenProperties: true })).toEqual(output);
});

it("should convert SearchPlaceIndexForTextResponse to a FeatureCollection with a multiple features when flattenProperties is false", () => {
const input: SearchPlaceIndexForTextResponse = {
Summary: {
Text: "grocery store",
Expand Down Expand Up @@ -139,7 +182,84 @@ describe("placeToFeatureCollection", () => {
expect(placeToFeatureCollection(input)).toEqual(output);
});

it("should convert SearchPlaceIndexForPositionResponse to a FeatureCollection with a multiple features", () => {
it("should convert SearchPlaceIndexForTextResponse to a FeatureCollection with multiple features when flattenProperties is true", () => {
const input: SearchPlaceIndexForTextResponse = {
Summary: {
Text: "grocery store",
DataSource: "Esri",
},
Results: [
{
Place: {
Geometry: {
Point: [1, 2],
},
AddressNumber: "1050",
},
PlaceId: "abc",
},
{
Place: {
Geometry: {
Point: [3, 3],
},
AddressNumber: "609",
},
Distance: 1,
},
{
Place: {
Geometry: {
Point: [5, 5],
},
AddressNumber: "575",
},
PlaceId: "def",
},
],
};
const output: FeatureCollection = {
type: "FeatureCollection",
features: [
{
type: "Feature",
id: "abc",
properties: {
"Place.AddressNumber": "1050",
},
geometry: {
type: "Point",
coordinates: [1, 2],
},
},
{
type: "Feature",
properties: {
"Place.AddressNumber": "609",
Distance: 1,
},
geometry: {
type: "Point",
coordinates: [3, 3],
},
},
{
type: "Feature",
id: "def",
properties: {
"Place.AddressNumber": "575",
},
geometry: {
type: "Point",
coordinates: [5, 5],
},
},
],
};
expect(placeToFeatureCollection(input, { flattenProperties: true })).toEqual(output);
});

it("should convert SearchPlaceIndexForPositionResponse to a FeatureCollection with a multiple features when flattenProperties is false", () => {
const input: SearchPlaceIndexForPositionResponse = {
Summary: {
Position: [5, 5],
Expand Down Expand Up @@ -228,6 +348,89 @@ describe("placeToFeatureCollection", () => {
expect(placeToFeatureCollection(input)).toEqual(output);
});

it("should convert SearchPlaceIndexForPositionResponse to a FeatureCollection with multiple features when flattenProperties is true", () => {
const input: SearchPlaceIndexForPositionResponse = {
Summary: {
Position: [5, 5],
DataSource: "Esri",
},
Results: [
{
Place: {
Geometry: {
Point: [5, 5],
},
AddressNumber: "1050",
},
PlaceId: "abc",
Distance: 0,
},
{
Place: {
Geometry: {
Point: [4, 4],
},
AddressNumber: "609",
},
PlaceId: "def",
Distance: 1,
},
{
Place: {
Geometry: {
Point: [3, 3],
},
AddressNumber: "575",
},
PlaceId: "ghi",
Distance: 2,
},
],
};
const output: FeatureCollection = {
type: "FeatureCollection",
features: [
{
type: "Feature",
id: "abc",
properties: {
"Place.AddressNumber": "1050",
Distance: 0,
},
geometry: {
type: "Point",
coordinates: [5, 5],
},
},
{
type: "Feature",
id: "def",
properties: {
"Place.AddressNumber": "609",
Distance: 1,
},
geometry: {
type: "Point",
coordinates: [4, 4],
},
},
{
type: "Feature",
id: "ghi",
properties: {
"Place.AddressNumber": "575",
Distance: 2,
},
geometry: {
type: "Point",
coordinates: [3, 3],
},
},
],
};
expect(placeToFeatureCollection(input, { flattenProperties: true })).toEqual(output);
});

it("should skip a feature in the converted FeatureCollection if it is missing a Point field", () => {
const input: SearchPlaceIndexForTextResponse = {
Summary: {
Expand Down
Loading

0 comments on commit 44b0292

Please sign in to comment.