|
1 | 1 | import type { Feature, FeatureCollection, Point } from 'geojson'; |
2 | 2 |
|
3 | | -import { isValidCoord } from '@/lib/coords'; |
| 3 | +import { isValidCoord, isZeroZeroCoord } from '@/lib/coords'; |
4 | 4 | import type { Attachment, Field, FieldOption, Observation } from '@/lib/db'; |
5 | 5 |
|
6 | 6 | export type ExportFormat = 'geojson' | 'csv'; |
@@ -47,32 +47,34 @@ export function observationsToGeoJson( |
47 | 47 | observations: Observation[], |
48 | 48 | context: ObservationExportContext = {}, |
49 | 49 | ): FeatureCollection { |
50 | | - const features: Array<Feature<Point | null>> = observations.map((obs) => { |
51 | | - const hasValidCoords = |
52 | | - obs.lat !== undefined && |
53 | | - obs.lon !== undefined && |
54 | | - isValidCoord(obs.lat, obs.lon); |
55 | | - |
56 | | - const tags = buildExportTags(obs, context); |
57 | | - |
58 | | - return { |
59 | | - type: 'Feature' as const, |
60 | | - geometry: hasValidCoords |
61 | | - ? { type: 'Point' as const, coordinates: [obs.lon!, obs.lat!] } |
62 | | - : null, |
63 | | - properties: { |
64 | | - ...tags, |
65 | | - docId: obs.localId, |
66 | | - remoteId: obs.remoteId, |
67 | | - category: |
68 | | - context.displayNamesByObservationId?.get(obs.localId) ?? |
69 | | - tags.category, |
70 | | - presetRefDocId: obs.presetRefDocId ?? tags.presetRefDocId, |
71 | | - createdAt: obs.createdAt, |
72 | | - updatedAt: obs.updatedAt, |
73 | | - }, |
74 | | - }; |
75 | | - }); |
| 50 | + const features: Array<Feature<Point | null>> = observations |
| 51 | + .filter((o) => !isZeroZeroCoord(o.lat, o.lon)) |
| 52 | + .map((obs) => { |
| 53 | + const hasValidCoords = |
| 54 | + obs.lat !== undefined && |
| 55 | + obs.lon !== undefined && |
| 56 | + isValidCoord(obs.lat, obs.lon); |
| 57 | + |
| 58 | + const tags = buildExportTags(obs, context); |
| 59 | + |
| 60 | + return { |
| 61 | + type: 'Feature' as const, |
| 62 | + geometry: hasValidCoords |
| 63 | + ? { type: 'Point' as const, coordinates: [obs.lon!, obs.lat!] } |
| 64 | + : null, |
| 65 | + properties: { |
| 66 | + ...tags, |
| 67 | + docId: obs.localId, |
| 68 | + remoteId: obs.remoteId, |
| 69 | + category: |
| 70 | + context.displayNamesByObservationId?.get(obs.localId) ?? |
| 71 | + tags.category, |
| 72 | + presetRefDocId: obs.presetRefDocId ?? tags.presetRefDocId, |
| 73 | + createdAt: obs.createdAt, |
| 74 | + updatedAt: obs.updatedAt, |
| 75 | + }, |
| 76 | + }; |
| 77 | + }); |
76 | 78 |
|
77 | 79 | return { |
78 | 80 | type: 'FeatureCollection', |
@@ -119,32 +121,34 @@ export function observationsToCsv( |
119 | 121 | const header = CSV_COLUMNS.join(','); |
120 | 122 | if (observations.length === 0) return header; |
121 | 123 |
|
122 | | - const rows = observations.map((obs) => { |
123 | | - const tags = buildExportTags(obs, context); |
124 | | - const photoUrls = tags.photoUrls ?? ''; |
125 | | - const category = |
126 | | - context.displayNamesByObservationId?.get(obs.localId) ?? |
127 | | - tags.category ?? |
128 | | - ''; |
129 | | - |
130 | | - const hasValidCoords = |
131 | | - obs.lat !== undefined && |
132 | | - obs.lon !== undefined && |
133 | | - isValidCoord(obs.lat, obs.lon); |
134 | | - |
135 | | - const values: string[] = [ |
136 | | - csvEscape(obs.localId), |
137 | | - csvEscape(category), |
138 | | - hasValidCoords ? String(obs.lat) : '', |
139 | | - hasValidCoords ? String(obs.lon) : '', |
140 | | - csvEscape(obs.createdAt), |
141 | | - csvEscape(obs.updatedAt), |
142 | | - csvEscape(JSON.stringify(tags)), |
143 | | - csvEscape(photoUrls), |
144 | | - ]; |
145 | | - |
146 | | - return values.join(','); |
147 | | - }); |
| 124 | + const rows = observations |
| 125 | + .filter((o) => !isZeroZeroCoord(o.lat, o.lon)) |
| 126 | + .map((obs) => { |
| 127 | + const tags = buildExportTags(obs, context); |
| 128 | + const photoUrls = tags.photoUrls ?? ''; |
| 129 | + const category = |
| 130 | + context.displayNamesByObservationId?.get(obs.localId) ?? |
| 131 | + tags.category ?? |
| 132 | + ''; |
| 133 | + |
| 134 | + const hasValidCoords = |
| 135 | + obs.lat !== undefined && |
| 136 | + obs.lon !== undefined && |
| 137 | + isValidCoord(obs.lat, obs.lon); |
| 138 | + |
| 139 | + const values: string[] = [ |
| 140 | + csvEscape(obs.localId), |
| 141 | + csvEscape(category), |
| 142 | + hasValidCoords ? String(obs.lat) : '', |
| 143 | + hasValidCoords ? String(obs.lon) : '', |
| 144 | + csvEscape(obs.createdAt), |
| 145 | + csvEscape(obs.updatedAt), |
| 146 | + csvEscape(JSON.stringify(tags)), |
| 147 | + csvEscape(photoUrls), |
| 148 | + ]; |
| 149 | + |
| 150 | + return values.join(','); |
| 151 | + }); |
148 | 152 |
|
149 | 153 | return [header, ...rows].join('\n'); |
150 | 154 | } |
|
0 commit comments