Created
May 5, 2024 00:05
-
-
Save doneill/8a378386a669cf96a1bbf82631aae3db to your computer and use it in GitHub Desktop.
Function to chunk large geometry tables from PostGIS to GeoJSON
This file contains hidden or 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
| async createGeoJson(id, geom, srid, values) { | |
| try { | |
| const limit = 100000; | |
| let offset = 0; | |
| let features = []; | |
| while (true) { | |
| const chunk = await this.db.oneOrNone(sql.createGeoJson, { | |
| id: id, | |
| geom: geom, | |
| srid: srid, | |
| table: values, | |
| limit: limit, | |
| offset: offset | |
| }); | |
| if (!chunk || !chunk.jsonb_build_object || !chunk.jsonb_build_object.features || chunk.jsonb_build_object.features.length === 0) { | |
| break; | |
| } | |
| const chunkFeatures = chunk.jsonb_build_object.features; | |
| features = features.concat(chunkFeatures); | |
| offset += limit; | |
| } | |
| const result = { | |
| type: 'FeatureCollection', | |
| features | |
| }; | |
| console.debug(`result: ${JSON.stringify(result)}`); | |
| return result; | |
| } catch (error) { | |
| console.error('Error in createGeoJson:', error); | |
| throw error; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment