Skip to content

Commit

Permalink
Fix importing of LOI properties (#1957)
Browse files Browse the repository at this point in the history
  • Loading branch information
gino-m authored Aug 11, 2024
1 parent 4cbea52 commit d080c09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 5 additions & 1 deletion functions/src/import-geojson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {GroundProtos} from '@ground/proto';

import Pb = GroundProtos.ground.v1beta1;
const l = registry.getFieldIds(Pb.LocationOfInterest);
const pr = registry.getFieldIds(Pb.LocationOfInterest.Property);
const g = registry.getFieldIds(Pb.Geometry);
const p = registry.getFieldIds(Pb.Point);
const c = registry.getFieldIds(Pb.Coordinates);
Expand Down Expand Up @@ -78,7 +79,10 @@ describe('importGeoJson()', () => {
},
[l.submissionCount]: 0,
[l.source]: 1, // IMPORTED
[l.properties]: {name: 'Dinagat Islands', area: 3.08},
[l.properties]: {
name: {[pr.stringValue]: 'Dinagat Islands'},
area: {[pr.numericValue]: 3.08},
},
jobId: 'job123',
predefined: true,
geometry: {type: 'Point', coordinates: TestGeoPoint(10.1, 125.6)},
Expand Down
22 changes: 19 additions & 3 deletions functions/src/import-geojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import {GroundProtos} from '@ground/proto';
import {Datastore} from './common/datastore';
import {DocumentData} from 'firebase-admin/firestore';
import {toDocumentData, deleteEmpty} from '@ground/lib';
import {Feature, Geometry, Position} from 'geojson';
import {Feature, GeoJsonProperties, Geometry, Position} from 'geojson';
import {ErrorHandler} from './handlers';

import Pb = GroundProtos.ground.v1beta1;
import {ErrorHandler} from './handlers';

/**
* Read the body of a multipart HTTP POSTed form containing a GeoJson 'file'
Expand Down Expand Up @@ -187,10 +187,26 @@ function toLoiPb(
customTag: id?.toString(),
source: Pb.LocationOfInterest.Source.IMPORTED,
geometry: geometryPb,
properties,
properties: toLoiPbProperties(properties),
});
}

function toLoiPbProperties(properties: GeoJsonProperties): {
[k: string]: Pb.LocationOfInterest.Property;
} {
return Object.fromEntries(
Object.entries(properties ?? {}).map(([k, v]) => [k, toLoiPbProperty(v)])
);
}

function toLoiPbProperty(value: any): Pb.LocationOfInterest.Property {
return new Pb.LocationOfInterest.Property(
typeof value === 'number'
? {numericValue: value}
: {stringValue: value.toString()}
);
}

function toGeometryPb(geometry: Geometry): Pb.Geometry {
switch (geometry.type) {
case 'Point':
Expand Down

0 comments on commit d080c09

Please sign in to comment.