-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: planning area grid custom piece exporter (#878)
* feat: planning area grid custom piece exporter * feat: add size property to exported planning area custom grid * fix: create one stream per file * ref: grid file format Co-authored-by: Ángel Higuera Vaquero <angelhiguera@acidtango.com>
- Loading branch information
1 parent
739f6d6
commit dd75809
Showing
3 changed files
with
139 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 67 additions & 1 deletion
68
api/libs/cloning/src/infrastructure/clone-piece-data/planning-area-grid-custom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,69 @@ | ||
export const planningAreaCustomGridRelativePath = 'project-grid.json'; | ||
import { Polygon } from 'geojson'; | ||
import { Transform, TransformCallback } from 'stream'; | ||
|
||
export const planningAreaCustomGridRelativePath = 'project-grid'; | ||
export const planningAreaCustomGridGeoJSONRelativePath = | ||
'project-grid/custom-grid.geojson'; | ||
|
||
export class PlanningAreaGridCustomTransform extends Transform { | ||
firstChunk = true; | ||
|
||
constructor() { | ||
super({ | ||
objectMode: true, | ||
}); | ||
} | ||
|
||
_transform( | ||
chunk: { ewkb: Buffer; puid: string }, | ||
encoding: BufferEncoding, | ||
callback: TransformCallback, | ||
): void { | ||
const record = { | ||
geom: chunk.ewkb.toJSON().data, | ||
puid: parseInt(chunk.puid), | ||
}; | ||
const content = `${record.puid},${JSON.stringify(record.geom)}`; | ||
const data = (this.firstChunk ? '' : '\n') + content; | ||
|
||
callback(null, data); | ||
|
||
if (this.firstChunk) { | ||
this.firstChunk = false; | ||
} | ||
} | ||
} | ||
|
||
export class PlanningAreaGridCustomGeoJsonTransform extends Transform { | ||
firstChunk = true; | ||
|
||
constructor(bbox: number[]) { | ||
super({ | ||
objectMode: true, | ||
}); | ||
this.push( | ||
`{ "type": "MultiPolygon", "bbox": [${bbox}], "coordinates": [\n`, | ||
); | ||
} | ||
|
||
_transform( | ||
chunk: { geojson: string }, | ||
encoding: BufferEncoding, | ||
callback: TransformCallback, | ||
): void { | ||
const polygon: Polygon = JSON.parse(chunk.geojson); | ||
const data = | ||
(this.firstChunk ? '' : ',\n') + JSON.stringify(polygon.coordinates); | ||
|
||
callback(null, data); | ||
|
||
if (this.firstChunk) { | ||
this.firstChunk = false; | ||
} | ||
} | ||
|
||
_flush(callback: TransformCallback): void { | ||
this.push(']}'); | ||
callback(); | ||
} | ||
} |