Skip to content

Instantly share code, notes, and snippets.

@LarsSchy
Last active November 12, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsSchy/644b37ab2b1e58b48808bfb2b57d0359 to your computer and use it in GitHub Desktop.
Save LarsSchy/644b37ab2b1e58b48808bfb2b57d0359 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
## showing usage of shp2tile, to create smaller shape files and tile index files for usage in Mapserver
#
## .............
# Split large shapefiles with shp2tile into quad structure
# Select shape files greater than 20 MB
if [ ! -d ${FULL_DIR_FINAL}/tindex ] ; then
echo "mkdir ${FULL_DIR_FINAL}/tindex"
mkdir ${FULL_DIR_FINAL}/tindex
fi
layers=$(ls -l ${FULL_DIR_FINAL}/??_????.shp | \
awk '{if ($5 > 20000000 ) print $9 }' | \
sed 's!.*/!!' | \
cut -d'.' -f1)
for LAYER in $layers
do
echo "Processing tiles for layer: $LAYER"
# Tile shape file with shp2tile
if [ -d ${FULL_DIR_FINAL}/${LAYER}_tiles ] ; then
rm -r ${FULL_DIR_FINAL}/${LAYER}_tiles
fi
mkdir ${FULL_DIR_FINAL}/${LAYER}_tiles
shp2tile \
--square-ext \
--quadtree 50000 \
--maxdepth 3 \
${FULL_DIR_FINAL}/${LAYER}.shp \
${FULL_DIR_FINAL}/${LAYER}_tiles/${LAYER}
# Reapply the right encoding that is lost in shp2tile command
echo "Reappling the right encoding"
origfiles=$(ls -1 ${FULL_DIR_FINAL}/${LAYER}_tiles)
mkdir ${FULL_DIR_FINAL}/${LAYER}_tiles/tmp
for file in ${FULL_DIR_FINAL}/${LAYER}_tiles/*.shp
do
DIR=$(dirname $file)
BASE=$(basename $file)
ogr2ogr \
$DIR/tmp/${BASE} \
${DIR}/${BASE} \
-lco ENCODING=UTF-8 \
--config SHAPE_ENCODING "ISO-8859-1" \
-progress
done
for file in $origfiles
do
rm ${FULL_DIR_FINAL}/${LAYER}_tiles/$file
done
mv ${FULL_DIR_FINAL}/${LAYER}_tiles/tmp/*.* ${FULL_DIR_FINAL}/${LAYER}_tiles
rm -r ${FULL_DIR_FINAL}/${LAYER}_tiles/tmp
# Create the tileindex files
# This has to be done relative to SHAPEPATH which is two levels up
DIR=$(pwd)
cd ${TARGET_DIR}
if [ -f ${FULL_DIR_FINAL}/tindex/${LAYER}_ti.shp ] ; then
rm ${FULL_DIR_FINAL}/tindex/${LAYER}_ti.shp
fi
# SET_DIR is set not the full path, just relative path from SHAPEPATH in Mapserver
ogrtindex \
${FULL_DIR_FINAL}/tindex/${LAYER}_ti.shp \
${SET_DIR}/${LAYER}_tiles/*.shp
shptree ${FULL_DIR_FINAL}/tindex/${LAYER}_ti.shp
cd ${FULL_DIR_FINAL}
# apply spatial indexing with shptree
find ${LAYER}_tiles/ -name \*.shp -exec shptree {} \;
# apply indexing on KKOD in the shapefiles (KKOD is the most used
# variable in the mapserver class expressions)
for file in ${FULL_DIR_FINAL}/${LAYER}_tiles/*.shp
do
table=$(basename -s .shp $file)
ogrinfo ${file} -sql "CREATE INDEX ON ${table} USING KKOD"
done
# remove the original layer shape files, since we have the quad layer now
rm ${FULL_DIR_FINAL}/${LAYER}.*
cd $DIR
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment