Skip to content

Commit

Permalink
Use mapshaper to simplify a region geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed May 31, 2024
1 parent 7a10252 commit 1f271c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/friction/load-friction-raster
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ for region in $regions; do
gdalwarp -co COMPRESS=DEFLATE \
-dstnodata -3.4e+38 \
-crop_to_cutline -csql "SELECT the_geom FROM regions WHERE id = ${region}" \
-cblend 1 \
-cutline PG:"dbname=${POSTGRES_DB} host=${POSTGRES_HOST} port=${POSTGRES_PORT} user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}" \
$raster_file $output_file
else
Expand Down
19 changes: 18 additions & 1 deletion scripts/population/load-regions
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,30 @@ if [[ ! -e ${DATA_PATH}/$COUNTRY_CODE ]]; then
exit 1
fi

# Simplification parameter
# From mapshaper wiki https://github.com/mbloch/mapshaper/wiki/Command-Reference#-simplify
#
# interval= Specify simplification amount in units of distance.
# Uses meters when simplifying unprojected datasets in 3D space (see planar option below),
# otherwise uses the same units as the source data.
INTERVAL_DISTANCE=${INTERVAL_DISTANCE-1000}

IFS=',';
for i in $LEVELS; do
FILE=${DATA_PATH}/${COUNTRY_CODE}/${COUNTRY_CODE}_adm${i}.geojson
echo -n " -> Processing $FILE ... "

OFILE=$FILE
if [[ ! -z $INTERVAL_DISTANCE ]]; then
# Simplify using distance
OFILE=${DATA_PATH}/${COUNTRY_CODE}/${COUNTRY_CODE}_adm${i}_${INTERVAL_DISTANCE}.geojson

mapshaper $FILE -simplify interval=$INTERVAL_DISTANCE -o $OFILE
fi

psql -q -d $POSTGRES_DB -U $POSTGRES_USER -h $POSTGRES_HOST -p $POSTGRES_PORT << SQL_SCRIPT
WITH data AS (SELECT \$$`cat $FILE`\$$::json AS fc)
WITH data AS (SELECT \$$`cat $OFILE`\$$::json AS fc)
INSERT INTO "regions" (country, name, admin_level, the_geom)
SELECT
'${COUNTRY_NAME}',
Expand Down

0 comments on commit 1f271c5

Please sign in to comment.