piektdiena, 2009. gada 10. jūlijs

Aerofoto no balticmaps.eu - skripts 1

Skripts, kas novelk ortofoto bildi vienai konkrētai vietai no balticmaps.eu piedāvātajām aerofotokartēm.

Lai darbotos, nepieciešams bash, wget un imagemagick.
Skriptam kā parametri ir jānorāda X un Y koordinātes pēc balticmaps.eu, vēlamais kartes nosaukums un kartes vienas malas izmērs kilometros.
X un Y koordinātes var iegūt arī no garuma un platuma grādiem, izmantojot proj4 utilītas. piem.
 echo 57.151101N  24.285018E  | cs2cs ??? 


Jāatceras gan, ka komanda "montage" spēj noēst ļoti daudz atmiņas, un jau kartes ģenerēšana ar izmēriem 12x12km ir ļoti resursietilpīga.

Un pats skripts:

#!/bin/bash
# Ortofoto vilkshanas skripts

# Pārbaudam argumentu skaitu - jābūt 3
if [ $# -lt 3 ]
then
echo -e "Jāievada 3 vai 4 argumenti - centra X un Y koordinātes, vēlamais kartes nosaukums un kartes garums(platums)km (jaadalaas ar 4) \\n, piemēram:\\n
bash karte2.sh 353300 6363319 Ventspils 8 \\n
uztaisīs Ventspils aerofoto karti ar 8x8km izmēriem "
exit
fi

# Solis ir 2km
SOLIS=2000

# Mazās bildes nosaukumam sākumā piekabināsim kārtas numuru, lai pēc tam vieglāk varētu kombinēt bildi.
# Vienkāršākais - sākas ar 101
NOS=101
centrsX=$1
centrsY=$2
nosaukums=$3
GaPl=$(($4/2)) 2>/dev/null
if [[ $GaPl == "" ]]
then
# minimālais kartes izmērs - 2x2 km
GaPl=1
fi

#Pārbaudam vai šāds fails jau ir uztaisīts, ja ir - pārsaucam, pieliekot datumu un laiku
if [ -f $nosaukums-map.jpg ]
then
echo Kartes fails ar šādu nosaukumu jau eksistē - pārvietojam to
mv $nosaukums-map.jpg $nosaukums-map-`date +%y%m%d-%H%M`.jpg
fi

tmpkartes=`mktemp -d ./tmp-$nosaukums.XXXXXXXX`

# Apalojam XY līdz veseliem 100 - tā labāk lasās no servera ?
centrsX=$((($centrsX+50)/100))00
centrsY=$((($centrsY+50)/100))00

## Funkcija, kas veic bildes vilkshanu un pārbauda, vai tā ir novilkta
function ortofoto {
echo velkam $X $Y $SOLIS $NOS
bilde=$tmpkartes/$NOS-$X-$Y
wget -q "http://maps.kartes.lv/kijs/server_scripts/orto/get_orto_foto.php?BBOX=$X,$Y,$(($X+$SOLIS)),$(($Y+$SOLIS))&WIDTH=$SOLIS&HEIGHT=$SOLIS&KIJS=BM45" -O $bilde.jpg || wget -q "http://maps.kartes.lv/kijs/server_scripts/orto/get_orto_foto.php?BBOX=$X,$Y,$(($X+$SOLIS)),$(($Y+$SOLIS))&WIDTH=$SOLIS&HEIGHT=$SOLIS&KIJS=BM45" -O $bilde.jpg
# Ja joprojaam bilde nav novilkta, tad meeginam no
if [[ `identify -format '%W%H' $bilde.jpg` != "$SOLIS$SOLIS" ]] 2>/dev/null ; then
# Uztaisam mazu nobiidi par 1 pikseli
rm $bilde.jpg 2>/dev/null
X=$(($X-1)) && Y=$(($Y-1))
wget -nv "http://maps.kartes.lv/kijs/server_scripts/orto/get_orto_foto.php?BBOX=$X,$Y,$(($X+$SOLIS)),$(($Y+$SOLIS))&WIDTH=$SOLIS&HEIGHT=$SOLIS&KIJS=BM45" -O $bilde-a.jpg
if [[ `identify -format '%W%H' $bilde-a.jpg` != "$SOLIS$SOLIS" ]] ; then
rm $bilde-a.jpg 2>/dev/null
wget -nv "http://maps.kartes.lv/kijs/server_scripts/orto/get_orto_foto.php?BBOX=$X,$Y,$(($X+$SOLIS)),$(($Y+$SOLIS))&WIDTH=$SOLIS&HEIGHT=$SOLIS&KIJS=BM45" -O $bilde-b.jpg
if [[ `identify -format '%W%H' $bilde-b.jpg` != "$SOLIS$SOLIS" ]] ; then
rm $bilde-b.jpg 2>/dev/null
# Izveidojam mazu bildes failu, kas nepieciešams tālākai bildes kopā komponēšanai
convert -size 10x10 xc:gray $bilde-c.jpg
fi
fi
fi
}

# Aprēķinam kreiso X koordināti
startX=$(($centrsX-$GaPl/2*$SOLIS))
# Aprēķinam augšējo Y koordināti
startY=$(($centrsY+$GaPl/2*$SOLIS))

# echo $startX $startY

# Velkam kartes - saakot no augšējā kreisā stūra
# visas mazo karshu apakseejaas Y koordinaates
for Y in `seq $(($startY-$SOLIS)) -$SOLIS $(($startY - $SOLIS*$GaPl))`
do

# visas mazo karshu kreisās X koordinātes
for X in `seq $startX $SOLIS $(($startX + $SOLIS*($GaPl-1)))` ; do
# nosuutam mazaas bildes datus kartes vilksanas f-jai
ortofoto $X $Y $SOLIS $NOS
NOS=$(($NOS+1))
done
done

# Montējam kopā failu
if which montage >/dev/null 2>/dev/null; then
montage -verbose -tile $GaPlx$GaPl -geometry $SOLISx$SOLIS $tmpkartes/*jpg ./$nosaukums-map.jpg
rm -r $tmpkartes
fi

ceturtdiena, 2009. gada 9. jūlijs

DNAT routing over second link

The problem:
  • You have 2 connnections to the Internet - A is the default link, B - secondary link.
  • You have hosts inside LAN which are DNAT'ed from B space IP addresses.
  • Provider A blocks IP addresses from B address space
  • on router you have DNAT'ed some hosts to LAN and SNAT'ed all outgoing IP addresses on A and B interfaces.
Although you have already build some ip routes un rules to answer to packets on exactly the same interfaces, where incoming connections occurs, the DNAT'ed LAN hosts routes all outgoing packets over default - A link with SNAT'ed addresses from B space.

To correct this, you should:
  • add corresponding iproute2 table (if not already exist)
  • mark all outgoing packets from specified LAN host(s) with iptables PREROUTING chain
  • add ip rule to route marked packets through specified table and gateway


## script - all other routes and configurations skipped
## you should add this only one time
# echo 200 bprovider >> /etc/iproute2/rt_tables
...
## add routes to B space and gateway - all in bprovider table
ip route add $B-NET dev $B-DEV src $B-IP table bprovider
ip route add default via $B-GW table bprovider

## add rule to answer all incoming connections to B space over B gateway
ip rule add from $B-IP table bprovider

## iptables part - DNAT incoming packets, SNAT outgoing packets, and mark LAN packets - only for B addresses.
## Only some http ports (80,443) are redirected to LAN host
...
iptables -t nat -A PREROUTING -d $B-IP -m multiport -p tcp --dports 80,443 -j DNAT --to $LAN_IP
iptables -t nat -A POSTROUTING -o $B-DEV -j SNAT --to-source $B-IP
iptables -t mangle -A PREROUTING -s $LAN_IP -j MARK --set-mark 99
...
## and finally - order marked packets to routed over B gateway
ip rule add fwmark 99 table bprovider

Some other approach with CONNMARK (don't tested yet): http://home.regit.org/?page_id=7

svētdiena, 2009. gada 5. jūlijs

Attēlu apstrāde ar imagemagick

Pāris skriptu, kā apstrādāt attēlu failus lielās direktorijās

Skripts, kas visiem attēliem faila nosaukumiem pieliek klāt datumu:

#!/bin/bash
for i in *jpg *JPG
do
# Izvelkam datumu no bildes
DATUMS=`identify -format '%[EXIF:DateTime]' $i | tr : _ | awk '{print $1}' `
# Ja datuma vērtība eksistē un bildes nosaukumā jau nau datums, tad pārsaucam bildi
if [[ -n $DATUMS && $i != $DATUMS* ]]
then
mv -v $i $DATUMS-$i
fi
done


Uzmanību Ubuntu lietotājiem: Dažās versijās ir bugs, kas neattēlo EXIF informāciju!
https://bugs.launchpad.net/ubuntu/+source/imagemagick/+bug/227631

ceturtdiena, 2009. gada 2. jūlijs

To enable routing on Ubuntu host

edit /etc/sysctl.conf or with sysctl -w

To allow forward packets
sysctl -w net.ipv4.ip_forward=1

To forward packets comming to "wrong" interfaces:
sysctl -w net.ipv4.conf.default.rp_filter=0
(filter is enabled by default)