Skip to content

Commit

Permalink
allow set fields lat long mapname from csv
Browse files Browse the repository at this point in the history
  • Loading branch information
erasta committed Nov 12, 2024
1 parent 80e56c1 commit a9147d8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Grid, Stack, Typography } from "@mui/material"
import { SelectProperty } from "../../Property/SelectProperty";
import { useEffect } from "react";
import { FIELD_UNASSIGNED, IGNORE_FIELDS } from "./uploadDefs";
import { FIELD_LATITUDE, FIELD_LONGITUDE, FIELD_MAPNAME, FIELD_UNASSIGNED, IGNORE_FIELDS } from "./uploadDefs";

function uniq(list) {
return list.reduce((acc, d) => acc.includes(d) ? acc : acc.concat(d), []);
Expand All @@ -12,7 +12,9 @@ export const UploadDevicesTypeFieldsMatcher = ({ devicesDetails, deviceType, att
return Object.keys(x.attributes).filter(f => !IGNORE_FIELDS.includes(f));
}));

const attributeTypeNames = deviceType?.attributeTypes?.map(x => x.name) || []
// TODO: enforce location fields
const attributeTypeNames = deviceType?.attributeTypes?.map(x => x.name) || [];
attributeTypeNames.unshift(FIELD_LATITUDE, FIELD_LONGITUDE, FIELD_MAPNAME);

useEffect(() => {
const matches = {};
Expand All @@ -22,7 +24,8 @@ export const UploadDevicesTypeFieldsMatcher = ({ devicesDetails, deviceType, att
setAttrMatch(matches);
}, [])

const attrOptions = fieldNamesOnDetails.map(f => ({ name: f })).concat([{ name: FIELD_UNASSIGNED }]);
const attrOptions = fieldNamesOnDetails.map(f => ({ name: f }));
attrOptions.push({ name: FIELD_UNASSIGNED });

return (
<Stack direction='column' spacing={1} sx={{ margin: 1 }}>
Expand Down
22 changes: 14 additions & 8 deletions client/src/IO/UploadDevices/changeDeviceOnTrial.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { SCOPE_TRIAL } from "../../Experiment/AttributeType";
import { FIELD_UNASSIGNED, DevicesFromFile } from "./uploadDefs";
import { FIELD_UNASSIGNED, DevicesFromFile, FIELD_MAPNAME, FIELD_LATITUDE, FIELD_LONGITUDE } from "./uploadDefs";

export const changeDeviceOnTrial = (
trial: { devicesOnTrial: any[]; },
experiment: { deviceTypes: any[]; },
deviceToUpload: DevicesFromFile,
attrMatch: { [key: string]: number; },
) => {
const { type, name, MapName, Latitude, Longitude, attributes } = deviceToUpload;
const { type, name, attributes } = deviceToUpload;
const deviceType = experiment?.deviceTypes?.find(x => x.name === type);
if (deviceType) {
trial.devicesOnTrial ||= [];
Expand All @@ -17,16 +17,22 @@ export const changeDeviceOnTrial = (
trial.devicesOnTrial.push(deviceOnTrial);
}

const attrMatchForType = attrMatch[deviceType.name];

const MapName = attributes[attrMatchForType[FIELD_MAPNAME]];
const Latitude = parseFloat(attributes[attrMatchForType[FIELD_LATITUDE]]);
const Longitude = parseFloat(attributes[attrMatchForType[FIELD_LONGITUDE]]);

if (!MapName || !isFinite(Latitude) || !isFinite(Longitude)) {
throw "Unparsable location on device: " + JSON.stringify(deviceToUpload);
}

deviceOnTrial.location = {
"name": MapName,
"coordinates": [
parseFloat(Latitude),
parseFloat(Longitude),
]
"coordinates": [Latitude, Longitude]
}

const attrMatchForType = Object.entries(attrMatch[deviceType.name]);
for (const [attrNameOnDev, attrNameFromFile] of attrMatchForType) {
for (const [attrNameOnDev, attrNameFromFile] of Object.entries(attrMatchForType)) {
if (attrNameFromFile || FIELD_UNASSIGNED !== FIELD_UNASSIGNED) {
const attrType = deviceType?.attributeTypes?.find(x => x.name === attrNameOnDev);
if (attrType && (attrType.scope || SCOPE_TRIAL) === SCOPE_TRIAL) {
Expand Down
14 changes: 7 additions & 7 deletions client/src/IO/UploadDevices/obtainDevicesFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ const obtainDeviceFromJson = (text: string): DevicesFromFile[] => {
const json = JSON.parse(text);
const devices: DevicesFromFile[] = [];
for (const dev of json.features) {
devices.push({
type: dev.properties.type,
name: dev.properties.name,
const attributes = {
...dev.properties,
MapName: dev.properties.MapName,
Latitude: dev.geometry.coordinates[1],
Longitude: dev.geometry.coordinates[0],
attributes: dev.properties
};
devices.push({
type: dev.properties.type,
name: dev.properties.name,
attributes,
});
}
return devices;
Expand All @@ -29,9 +32,6 @@ const obtainDeviceFromCsv = (text: string): DevicesFromFile[] => {
devices.push({
type: dev.type,
name: dev.name,
MapName: dev.MapName,
Latitude: dev.Latitude,
Longitude: dev.Longitude,
attributes: dev
});
}
Expand Down
3 changes: 0 additions & 3 deletions client/src/IO/UploadDevices/uploadDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ export const IGNORE_FIELDS = [FIELD_TYPE, FIELD_TYPE];
export interface DevicesFromFile {
type: string;
name: string;
MapName: string;
Latitude: string;
Longitude: string;
attributes: any;
}

0 comments on commit a9147d8

Please sign in to comment.