stats
stats

--- /dev/null
+++ b/scorpion_STATS/.gitignore
@@ -1,1 +1,3 @@
+credentials
+out/
 

--- /dev/null
+++ b/scorpion_STATS/run_all.sh
@@ -1,1 +1,77 @@
+#!/bin/bash
 
+
+OSM_BIN=/media/ssd/osm-3s_v0.7.52/bin
+BULKUTILS=../bulkupload
+
+#UPU: OSM user
+#UPP: OSM pass
+source credentials
+
+UPM="scorpion water tag changes. See wiki/User:FrankVD_bot"
+
+
+ODIR=/dev/shm/scorpion
+ODIROSC=out/
+
+
+echo "Will generate relations"
+echo "TODO: parallelize the thing"
+mkdir $ODIR/
+mkdir $ODIROSC/
+rm -rf $ODIR/*
+#rm -rf $ODIROSC/*
+
+YFROM=-56
+YTO=80
+TSTEP=1
+for Y in `seq $YFROM $TSTEP $YTO`; do
+	for X in `seq -180 $TSTEP 179`; do
+		X1=$X
+		Y1=$Y
+		X2=$(($X+$TSTEP))
+		Y2=$(($Y+$TSTEP))
+		BOX="($Y1, $X1, $Y2, $X2)"
+		FNAME='rel_'$X1'_'$Y1'_'$X2'_'$Y2
+
+		EXNAME=$ODIR/$FNAME.osm
+		FIXNAME=$ODIR/corr_$FNAME.osm
+		OSCTMP=$ODIR/corr_$FNAME.osc
+		OSCNAME=$ODIROSC/corr_$FNAME.osc
+		echo $FNAME
+		if [[ -e $OSCNAME ]]; then
+			echo "continue";
+			continue
+		fi;
+#		[timeout:3600]
+#		[maxsize:6073741824];
+		$OSM_BIN/osm3s_query > $EXNAME 2>/dev/null << EOF
+		(
+			relation
+			["type"="multipolygon"]
+			["natural"="water"]
+			$BOX;
+		);
+		out meta;
+		>;
+		out meta;
+EOF
+
+		php scorpion.php $EXNAME $FIXNAME >> logs
+		$BULKUTILS/osm2change.py $FIXNAME
+		mv $OSCTMP $OSCNAME
+
+		sum=`md5sum $OSCNAME | cut -d' ' -f1`
+		if [[ "$sum" != "4cd6b83fec8a85134d19f623454b6174" ]]; then
+			UPM2="$UPM . (run on $BOX)"
+		else
+			> $OSCNAME #to zero. so it doesn't take place
+		fi;
+
+		rm $ODIR/$FNAME.osm
+		rm $ODIR/corr_$FNAME.osm
+	done
+	echo "Progress: $Y/$YTO"
+done
+
+

--- /dev/null
+++ b/scorpion_STATS/scorpion.php
@@ -1,1 +1,160 @@
+#!/usr/bin/php
+<?php
+/*
+** scorpion.php
+**
+** Author:      Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
+** Description: Removes trivial 'natural=water' tags from non closed ways
+**              which are on a relation.
+*/
 
+ini_set('memory_limit','10G');
+
+
+if($argc < 3) {
+	die("USAGE: php multi2poly.php FILE_IN.osm FILE_OUT.osm\n");
+}
+$infile = $argv[1];
+$outfile = $argv[2];
+
+$xmlc = file_get_contents($infile);
+
+$xml = new SimpleXMLElement($xmlc);
+
+function find_way_by_id($xml, $way_id) { //{{{
+	//No hashing table… Meh, someday.
+	foreach($xml->way as $way) {
+		if(intval($way->attributes()->id) == $way_id) {
+			return $way;
+		}
+	}
+	return NULL;
+}//}}}
+
+/*
+ * Converts an XML way to an array of nodes
+ */
+function way_to_Fway($way) { //{{{
+	$nodes = array();
+	foreach($way->nd as $node) {
+		array_push($nodes, intval($node->attributes()->ref));
+	}
+	return $nodes;
+}
+//}}}
+/*
+ * Is the Fway (array of points) closed ?
+ */
+function is_Fway_closed($Fway) { //{{{
+	if($Fway[0] == end($Fway))
+		return true;
+	return false;
+} //}}}
+
+/*
+ * Get the tag of an XML element
+ */
+function get_tag(&$parent_tags, $key) { //{{{
+	foreach($parent_tags->tag as $tag) {
+		if($tag['k'] == $key) {
+			return $tag['v'];
+		}
+	}
+	return NULL;
+}//}}}
+
+/*
+ * Delete a tag from an XML element AND append the action=modify to it
+ */
+function delete_tag(&$parents_tags, $key) {//{{{
+	$i = 0;
+	foreach($parents_tags->tag as $tag) {
+		if($tag['k'] == $key) {
+			if($parents_tags->tag[$i] != $tag) {
+				die('error delete');
+			}
+
+			unset($parents_tags->tag[$i]);
+			if(!isset($parents_tags->attributes()->action))
+				$parents_tags->addAttribute('action', 'modify');
+
+			return;
+		}
+		$i++;
+	}
+}//}}}
+
+
+
+
+/*
+ * Relations given to scorpion were exported with the natural=water tag,
+ * so they should all have this !. We know that we can take tags out of the ways
+ * as they are on their relation.
+ */
+
+foreach($xml->relation as $relation) {
+	$relid = $relation->attributes()->id;
+
+	//Make sure that the relation is a water surface
+	if(!get_tag($relation->tag, 'natural') != 'water') {
+		echo "I had: ".get_tag($relation, 'natural')."\n";
+		die('Should never happen as we export only nat=wat relations');
+	}
+
+
+	$water_rela = get_tag($relation, 'water');
+	//if($water_rela)
+		//echo "relation has water=$water_rela\n";
+
+	$had_outer = false;
+
+	foreach($relation->member as $member) {
+		if($member['role'] == 'outer')
+			$had_outer = true;
+
+/*		if($member['role'] != 'outer' && $had_outer) {
+			echo "Will return from relation $relid\n";
+			break; //Don't proccess ring 2,3…
+		}
+ */
+
+		if($member['type'] != 'way' && $member['type'] != 'relation') {
+			//echo "My member type is: ".$member['type']." for -> $relid\n";
+			continue;
+		}
+
+		$wayid = $member['ref'];
+		$way = find_way_by_id($xml, $wayid);
+
+		if($way == NULL) {
+			//echo "Problem getting $wayid. Shono happen'\n";
+			continue;
+		}
+		$wayF = way_to_Fway($way);
+
+		if(!is_Fway_closed($wayF)) {
+			//Now is our interesting part. We know that this way is not closed
+			//Now, we can delete it's natural=water key if it has it
+			
+			if(!strcmp(get_tag($way, 'natural'), 'water')) {
+				echo "Way $wayid had natural=water; source=".get_tag($way, 'source')."\n";
+				delete_tag($way, 'natural');
+			}
+		}
+
+		$way_wat = get_tag($way, 'water');
+		if($way_wat && !strcmp($way_wat, $water_rela)) {
+			echo "Way $wayid had water=$water_rela; source=".get_tag($way, 'source')."\n";
+			delete_tag($way, 'water');
+		}
+
+	} // }}}
+}
+
+file_put_contents($outfile, $xml->asXML());
+
+
+/* vim: set ts=4 sw=4 noet: */
+
+