Skip to content

Instantly share code, notes, and snippets.

@doneill
Created May 5, 2024 00:05
Show Gist options
  • Select an option

  • Save doneill/8a378386a669cf96a1bbf82631aae3db to your computer and use it in GitHub Desktop.

Select an option

Save doneill/8a378386a669cf96a1bbf82631aae3db to your computer and use it in GitHub Desktop.
Function to chunk large geometry tables from PostGIS to GeoJSON
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