Skip to content

Commit

Permalink
feat: replace turf
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Jul 16, 2024
1 parent 08aab06 commit d714139
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 1,602 deletions.
4 changes: 4 additions & 0 deletions ios/App/App/capacitor.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"appId": "de.reedu.senseboxbike",
"appName": "senseBox:Bike",
"webDir": "dist",
"server": {
"url": "http://192.168.2.136:5173",
"cleartext": true
},
"android": {
"useLegacyBridge": true
},
Expand Down
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@
"@radix-ui/react-tooltip": "^1.1.2",
"@react-spring/web": "^9.7.3",
"@remixicon/react": "^4.2.0",
"@tanstack/react-router": "^1.44.4",
"@turf/turf": "^6.5.0",
"@tanstack/react-router": "^1.45.2",
"@turf/bbox": "^7.0.0",
"@turf/boolean-point-in-polygon": "^7.0.0",
"@turf/buffer": "^7.0.0",
"@turf/helpers": "^7.0.0",
"autoprefixer": "^10.4.19",
"axios": "^1.6.8",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"danfojs": "1.1.1",
"date-fns": "^3.6.0",
"eslint": "9.6.0",
"eslint": "9.7.0",
"eslint-config-prettier": "^9.1.0",
"i18next": "^23.11.5",
"i18next": "^23.12.1",
"jszip": "^3.10.1",
"lodash.debounce": "^4.0.8",
"lucide-react": "^0.408.0",
Expand All @@ -68,13 +71,13 @@
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hook-form": "^7.52.1",
"react-i18next": "^14.1.2",
"react-i18next": "^14.1.3",
"react-map-gl": "^7.1.7",
"recharts": "2.13.0-alpha.4",
"reflect-metadata": "^0.2.2",
"swiper": "^11.1.1",
"swiper": "^11.1.5",
"tailwind-merge": "^2.4.0",
"tailwindcss": "^3.4.3",
"tailwindcss": "^3.4.5",
"tailwindcss-animate": "^1.0.7",
"typeorm": "^0.3.20",
"typescript": "5.5.3",
Expand All @@ -88,7 +91,7 @@
"@capacitor/assets": "^3.0.5",
"@capacitor/cli": "^6.1.0",
"@eslint/compat": "^1.1.1",
"@eslint/js": "^9.6.0",
"@eslint/js": "^9.7.0",
"@types/lodash.clonedeep": "^4.5.9",
"@types/lodash.debounce": "^4.0.9",
"@types/mapbox__mapbox-gl-draw": "^1.4.6",
Expand All @@ -99,19 +102,19 @@
"@types/react-router-dom": "^5.3.3",
"@types/turf": "^3.5.32",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "^7.16.0",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
"@vitejs/plugin-react": "^4.3.0",
"eslint-plugin-react": "^7.34.3",
"eslint-plugin-react": "^7.34.4",
"eslint-plugin-unused-imports": "^4.0.0",
"globals": "^15.8.0",
"husky": "^9.0.11",
"prettier": "^3.2.5",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.5",
"tailwindcss-safe-area": "^0.5.1",
"typescript-eslint": "^7.16.0",
"vite": "^5.3.3",
"typescript-eslint": "^7.16.1",
"vite": "^5.3.4",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.0.1"
"vitest": "^2.0.3"
}
}
3 changes: 1 addition & 2 deletions src/components/Device/DeviceMapWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export default function DeviceMapWrapper() {
// }
// }}
>
<DrawerContent className="h-full overflow-y-auto">
<div className="mt-2" />
<DrawerContent className="h-full">
<ControlBar ref={controlBarRef} />
<MeasurementsGrid ref={drawerContentRef} />
</DrawerContent>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Device/ExclusionZoneDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
DialogTrigger,
} from '../ui/dialog'

import { bbox, featureCollection } from '@turf/turf'
import bbox from '@turf/bbox'
import { featureCollection } from '@turf/helpers'
import { EarthLock } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { MapRef } from 'react-map-gl'
Expand Down
2 changes: 1 addition & 1 deletion src/components/Device/TrajectoryMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function TrajectoryMap() {
mapRef.current?.flyTo({
center,
zoom,
pitch: 45,
pitch: 55,
})
}
}, [trajectory])
Expand Down
5 changes: 1 addition & 4 deletions src/components/Map/ControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ const ControlBar = forwardRef<HTMLDivElement>((_, ref) => {
const { t } = useTranslation('translation', { keyPrefix: 'controls' })

return (
<div
className="flex w-full justify-between gap-2 p-2 pb-safe-or-4"
ref={ref}
>
<div className="flex w-full justify-between gap-2 p-2 py-4" ref={ref}>
<SettingsDrawer />
{!isConnected ? (
<div className="mx-auto flex w-full max-w-xl rounded-md bg-secondary">
Expand Down
4 changes: 2 additions & 2 deletions src/lib/exclusion-zone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { inside } from '@turf/turf'
import { booleanPointInPolygon } from '@turf/boolean-point-in-polygon'
import { useExclusionZoneStore } from './store/useExclusionZoneStore'

export function isInExclusionZone(point: GeoJSON.Feature<GeoJSON.Point>) {
Expand All @@ -7,6 +7,6 @@ export function isInExclusionZone(point: GeoJSON.Feature<GeoJSON.Point>) {
if (ez.features.length === 0) return false

return ez.features.some(feature => {
return inside(point, feature)
return booleanPointInPolygon(point, feature)
})
}
101 changes: 0 additions & 101 deletions src/lib/exporter/OpenSenseMapExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,106 +51,5 @@ export class OpenSenseMapExporter
await Filesystem.deleteFile({
path: mywriteResult.uri,
})

// const rowCount = df.shape[0]

// // Identify geolocation changes
// // Add a column to identify geolocation changes

// // Iterate through the DataFrame and assign groups
// let currentGroup = 0
// let lastLat = 0
// let lastLon = 0
// const series = []
// for (let i = 0; i < rowCount; i++) {
// const row = df.iloc({ rows: [i] }).values[0] as number[]
// const latitude = row[1]
// const longitude = row[2]
// if (
// latitude !== null &&
// (latitude !== lastLat || longitude !== lastLon)
// ) {
// currentGroup++
// lastLat = latitude
// lastLon = longitude
// }
// series.push(currentGroup)
// }
// df.addColumn('geo_id', series, { inplace: true })

// // danfo js timestamp to unix timestamp
// df.addColumn(
// 'timestamp_unix',
// df['timestamp'].apply((x: string) => new Date(x).getTime()),
// {
// inplace: true,
// },
// )

// // Specify aggregation functions for each column
// const aggregationFunctions: { [key: string]: any } = {
// timestamp_unix: 'min',
// latitude: 'max',
// longitude: 'max',
// overtaking: 'sum',
// finedust_pm1: 'mean',
// finedust_pm2_5: 'mean',
// finedust_pm4: 'mean',
// finedust_pm10: 'mean',
// distance: 'sum',
// humidity: 'mean',
// accelerometer_x: 'mean',
// accelerometer_y: 'mean',
// accelerometer_z: 'mean',
// temperature: 'mean',
// }

// // Group by geolocation and apply aggregations
// // TODO: Exclude null values from aggregation
// const grouped = df.groupby(['geo_id']).agg(aggregationFunctions)

// // calculate renamed columns by aggregation functions
// const renamedColumns: { [key: string]: string } = {}
// for (const column of Object.keys(aggregationFunctions)) {
// renamedColumns[`${column}_${aggregationFunctions[column]}`] = column
// }

// // timestamp_unix_min to date
// grouped.addColumn(
// 'timestamp',
// grouped['timestamp_unix_min'].apply((x: number) =>
// new Date(x).toISOString(),
// ),
// {
// inplace: true,
// },
// )

// // Drop the helper columns
// grouped.drop({ columns: ['geo_id', 'timestamp_unix_min'], inplace: true })

// // Rename the columns
// grouped.rename(renamedColumns, {
// inplace: true,
// })

// // delete rows with NaN values
// grouped.dropNa({ axis: 1, inplace: true })

// const csv = dfd.toCSV(grouped)

// const writeResult = await Filesystem.writeFile({
// data: csv as string,
// path: `senseBox_bike_${format(metadata.start, 'yyyy-MM-dd_HH-mm')}.csv`,
// directory: Directory.Cache,
// encoding: Encoding.UTF8,
// })
// await Share.share({
// url: writeResult.uri,
// })
// // cleanup
// await Filesystem.deleteFile({
// path: writeResult.uri,
// })
}
}
9 changes: 9 additions & 0 deletions src/lib/exporter/live/opensensemap-live-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import getAggregatedData from '@/lib/db/aggregation'
import { Track } from '@/lib/db/entities'
import { Upload } from '@/lib/db/entities/upload'
import senseBoxBikeDataSource from '@/lib/db/sources/senseBoxBikeDataSource'
import { isInExclusionZone } from '@/lib/exclusion-zone'
import { getTitlefromSensorKey } from '@/lib/senseBoxSensorIdMatcher'
import { useAuthStore } from '@/lib/store/useAuthStore'
import { point } from '@turf/helpers'
import * as dfd from 'danfojs'
import { DataFrame } from 'danfojs/dist/danfojs-base'
import { DataSource, Repository } from 'typeorm'
Expand Down Expand Up @@ -165,6 +167,13 @@ export function dfToOsemCSV(df: dfd.DataFrame) {
continue
}

if (isInExclusionZone(point([longitude, latitude]))) {
console.log(
'--- OSEM --- Skipping row because it is in the exclusion zone',
)
continue
}

console.log('--- OSEM --- Uploading data: ')
console.log(timestamp, latitude, longitude)

Expand Down
48 changes: 0 additions & 48 deletions src/lib/useRecordTrack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,56 +119,8 @@ const useRecordTrack = () => {

if (!selectedBox || !lcurrentTrackIdRef.current) {
return
// throw new Error('No box selected.')
}
exportData(lcurrentTrackIdRef.current)
// const uploader = OpenSenseMapLiveExporter.getInstance(currentTrackId)
// await uploader.export()

// if (!valuesRef.current) {
// throw new Error('No values.')
// }

// const data = valuesRef.current
// .filter(
// record => !isInExclusionZone(point([record.gps_lng!, record.gps_lat!])),
// )
// .flatMap(record => match(selectedBox, record))
// .map(record => ({
// ...record,
// value: record.value.toFixed(2),
// }))
// .slice(-2500) // max data to upload

// let filteredData = data

// if (lastUploadRef.current && uploadStartRef.current) {
// filteredData = data.filter(
// record =>
// new Date(record.createdAt).getTime() >
// uploadStartRef.current!.getTime() &&
// new Date(record.createdAt).getTime() >
// lastUploadRef.current!.getTime(),
// )
// }

// if (filteredData.length === 0) {
// console.log('No new data to upload.')
// return
// }

// try {
// setIsLoading(true)
// await uploadData(selectedBox, filteredData)
// const maxTimestamp = new Date(
// Math.max(...data.map(record => new Date(record.createdAt).getTime())),
// )
// setLastUpload(maxTimestamp)
// } catch (error) {
// console.error(error)
// } finally {
// setIsLoading(false)
// }
}

return { isRecording: intervalId !== undefined, isLoading, start, stop }
Expand Down
4 changes: 3 additions & 1 deletion src/pages/tracks/detail/detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import { CSVExporter } from '@/lib/exporter/CSVExporter'
import { OpenSenseMapExporter } from '@/lib/exporter/OpenSenseMapExporter'
import { ZipExporter } from '@/lib/exporter/ZIPExporter'
import { Link, useNavigate } from '@tanstack/react-router'
import { buffer, bbox, featureCollection, point } from '@turf/turf'
import bbox from '@turf/bbox'
import buffer from '@turf/buffer'
import { featureCollection, point } from '@turf/helpers'
import { format } from 'date-fns'
import { AudioWaveform, FileDownIcon, HomeIcon, Loader2 } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
Expand Down
Loading

0 comments on commit d714139

Please sign in to comment.