#! /bin/bash
# Last edited on 2013-03-02 03:20:30 by stolfilocal

# Usage: ${0##*/} {OBJ_ID} {OBJ_NAME}
# Fetches the Wikimapia object with ID number {OBJ_ID}
# and saves it to file obj/{OBJ_NAME}.xml, 
# with some beutifications.

id="$1"; shift
fname="$1"; shift

# Make sure any directories exist:
ofile="obj/${fname}.xml"
mkdir -p "${ofile%/*}"

if [[ -s ${ofile} ]]; then 
  mv -v ${ofile} ${ofile}~
fi

key="A8A59194-E164FD2E-4908BD67-C310F5CD-B242E9CD-5B2652DF-C4B53883-2814DA37"

wget "http://api.wikimapia.org/?function=object&key=${key}&id=${id}" -O - \
  | sed \
      -e 's:<point:\n  <point:g' \
      -e 's:</polygon:\n</polygon:g' \
  > ${ofile}
