Using real-world coordinates
authorMagnus Manske <magnusmanske@users.mediawiki.org>
Fri, 14 Jan 2005 12:30:01 +0000 (12:30 +0000)
committerMagnus Manske <magnusmanske@users.mediawiki.org>
Fri, 14 Jan 2005 12:30:01 +0000 (12:30 +0000)
experiments/geo/geo.php
experiments/geo/geo_functions.php [new file with mode: 0644]
experiments/geo/geomaker.php

index 2776117..e84a71c 100755 (executable)
@@ -2,32 +2,45 @@
 
 $a = array (
 
-"germany-bavaria-bw" => "!data:231,458 237,455 239,449 248,449 240,440 250,438 257,440 257,446 263,444 267,447 270,457 274,461 280,459 284,463 285,473 287,487 290,493 295,498 302,509 303,525 296,530 294,529 295,534 295,542 285,547 283,547 279,553 285,567 286,582 286,591 284,601 285,612 275,612 266,612 258,618",
-"germany-bavaria-hesse" => "!data:230,455 229,449 231,442 227,431 222,416 231,412 243,411 251,412 250,403 257,401 262,396 265,386 272,388 278,380",
-"germany-bavaria-thuringia" => "!data:280,376 288,376 298,388 306,391 308,402 319,399 314,392 321,388 335,390 338,396 343,392 341,381 348,377 352,384 356,388 376,384",
-"germany-bavaria-saxony" => "!data:378,388 382,391 388,394",
-"germany-bavaria-east" => "!data:387,400 394,408 406,417 412,425 404,438 420,463 434,468 444,479 453,485 464,498 470,498 481,505 485,519 480,533 468,530 467,537 463,548 454,555 443,557 436,564 430,571 437,578 442,590 444,602 447,603 449,607 447,620",
-"germany-bavaria-south" => "!data:447,620 436,618 435,609 427,605 419,610 412,605 404,607 403,601 398,605 402,613 381,616 369,616 366,621 358,622 359,627 350,630 346,627 340,631 332,630 329,622 314,618 309,622 304,618 306,627 306,631 293,642 288,644 292,636 286,637 284,630 271,622 268,619 266,624 260,621",
-
-"germany-bw-south" => "!data:253,618 247,612 240,610 227,598 234,612 222,604 224,610 218,614 211,607 207,607 207,604 201,602 193,603 192,611 195,614 204,611 201,617 193,619 186,619 184,615 176,617 174,620 168,622 165,622 160,617 151,622",
-"germany-bw-west" => "!data:143,610 144,597 147,585 145,577 153,557 156,546 161,534 169,523 175,516 180,504",
-"germany-bw-rp" => "!data:183,503 190,493 197,480 196,467 194,467 192,455",
-"germany-bw-hesse" => "!data:193,454 201,459 202,453 207,452 207,459 215,460 217,467 228,457",
-
-"germany-bavaria" => "
+"germany.bavaria/bw" => "!data:484627,092026 484900,092638 485407,092842 485407,093801 490146,092945 490329,094006 490146,094720 485640,094720 485822,095333 485549,095741 484718,100048 484354,100456 484536,101109 484212,101517 483341,101619 482146,101824 481639,102130 481224,102640 480302,103355 474925,103457 474509,102743 474600,102538 474145,102640 473456,102640 473041,101619 473041,101415 472535,101007 471339,101619 470053,101721 465314,101721 464443,101517 463521,101619 463521,100558 463521,095639 463014,094822",
+"germany.bavaria/hesse" => "!data:484900,091923 485407,091821 490004,092026 490926,091617 492212,091107 492537,092026 492628,093251 492537,094108 493316,094006 493458,094720 493914,095231 494745,095537 494602,100252 495251,100905",
+"germany.bavaria/thuringia" => "!data:495615,101109 495615,101926 494602,102947 494329,103804 493407,104008 493641,105131 494238,104620 494602,105335 494420,110805 493914,111111 494238,111622 495200,111417 495524,112132 494927,112541 494602,112949 494927,115031",
+"germany.bavaria/saxony" => "!data:494602,115235 494329,115644 494056,120256",
+"germany.bavaria/east" => "!data:493549,120154 492901,120909 492121,122134 491433,122747 490329,121930 484212,123604 483756,125033 482834,130054 482328,131013 481224,132137 481224,132749 480626,133912 475431,134321 474236,133810 474509,132545 473912,132443 472950,132034 472352,131115 472210,125952 471613,125238 471015,124625 470418,125340 465405,125850 464352,130054 464301,130401 463936,130605 462832,130401",
+"germany.bavaria/south" => "!data:462832,130401 463014,125238 463754,125135 464118,124319 463703,123502 464118,122747 463936,121930 464443,121828 464118,121317 463430,121726 463157,115542 463157,114316 462741,114010 462650,113153 462235,113255 462001,112336 462235,111928 461910,111315 462001,110458 462650,110152 463014,104620 462650,104110 463014,103559 462235,103804 461910,103804 460949,102436 460806,101926 461455,102334 461404,101721 462001,101517 462650,100150 462923,095844 462508,095639 462741,095027",
+
+"germany.bw/south" => "!data:463014,094312 463521,093659 463703,092945 464716,091617 463521,092332 464210,091107 463703,091311 463339,090658 463936,085943 463936,085535 464210,085535 464352,084922 464301,084106 463612,084003 463339,084310 463612,085229 463106,084922 462923,084106 462923,083351 463248,083147 463106,082330 462832,082125 462650,081513 462650,081207 463106,080656 462650,075737",
+"germany.bw/west" => "!data:463703,074920 464807,075022 465820,075329 470509,075124 472210,075941 473132,080248 474145,080758 475107,081615 475704,082228 480717,082738",
+"germany.bw/rp" => "!data:480808,083044 481639,083759 482743,084514 483847,084412 483847,084208 484900,084003",
+"germany.bw/hesse" => "!data:484951,084106 484536,084922 485042,085024 485133,085535 484536,085535 484445,090352 483847,090556 484718,091719",
+
+"germany.nrw/west" => "!data:515435,072940 514747,072021 514240,071306 513734,072021 513045,071409 513045,065531 513136,064305 512903,063244 511850,063551 510928,064305 510057,064612 504447,063857 504356,064305 503252,062529 502745,063346 502006,063551 501641,063142 501044,063755 500304,064612 495758,064101 495342,064816 494836,064918 494511,065122",
+"germany.nrw/rp" => "!data:494602,065020 494511,065428 494420,065939 494420,070552 494420,071409 495109,071000 495524,071306 495109,071715 495342,071919 495524,072021 500213,073655 500810,074614 500953,075022 501226,075941 501732,080350 502421,080656 502006,081207 501641,081411 501459,081717 500902,082023 500537,082432",
+"germany.nrw/hesse" => "!data:500628,082534 500902,082636 501317,082738 502006,083555 501732,084208 502148,084514 503252,084718 503343,085127 503434,085943 504214,085841 504538,085943 504538,085229 504811,085024 505409,085637 505551,090902 510148,090800 510422,091719 505733,091923 510057,092740 511110,093353",
+"germany.nrw/ns" => "!data:511343,093353 511941,093251 512812,093801 512903,093047 513551,092434 514604,092230 514656,091413 515344,091719 515526,091107 520033,091617 520448,091923 521228,091821 520630,090902 520357,090148 521319,085739 521319,085331 520904,083901 520306,084310 515344,084514 514929,084616 514422,083555 514240,082228 514149,081819 514422,081615 514838,082125 515111,081615 515800,082023 520357,081717 520306,080900 520955,080145 520448,080145 520124,075431 515617,074308 515526,073144",
+
+"germany.nrw" => "
+!type[political]:state
+!name[de]:Nordrhein-Westphalen
+!name[en]:North Rhine-Westphalia
+!region[political]:
+polygon(germany.nrw/west,germany.nrw/rp,germany.nrw/hesse,germany.nrw/ns)
+",
+
+"germany.bavaria" => "
 !type[political]:state
 !name[de]:Bayern
 !name[en]:Bavaria
 !region[political]:
-polygon(germany-bavaria-hesse,germany-bavaria-thuringia,germany-bavaria-saxony,germany-bavaria-east,germany-bavaria-south,germany-bavaria-bw)
+polygon(germany.bavaria/hesse,germany.bavaria/thuringia,germany.bavaria/saxony,germany.bavaria/east,germany.bavaria/south,germany.bavaria/bw)
 ",
 
-"germany-bw" => "
+"germany.bw" => "
 !type[political]:state
 !name[de]:Baden-Württemberg
 !name[en]:Baden-Württemberg
 !region[political]:
-polygon(germany-bavaria-bw,germany-bw-south,germany-bw-west,germany-bw-rp,germany-bw-hesse)
+polygon(germany.bavaria/bw,germany.bw/south,germany.bw/west,germany.bw/rp,germany.bw/hesse)
 ",
 
 "germany" => "
@@ -35,7 +48,7 @@ polygon(germany-bavaria-bw,germany-bw-south,germany-bw-west,germany-bw-rp,german
 !name[de]:Deutschland
 !name[en]:Germany
 !region[political]:
-addregs(germany-bavaria,germany-bw)
+addregs(germany.bavaria,germany.bw,germany.nrw)
 ",
 
 "danube" => "
@@ -48,6 +61,8 @@ addregs(germany-bavaria,germany-bw)
 
 ) ;
 
+include_once ( "geo_functions.php" ) ;
+
 # Global, evil variables
 $min_x = $min_y = 1000000 ;
 $max_x = $max_y = -1000000 ;
@@ -61,8 +76,14 @@ function geo_get_text ( $id )
 
 function data_to_real ( &$x , &$y )
        {
-#      $x = $x * 100 ;
-#      $y = $y * 100 ;
+       $x = coordinate_to_number ( coordinate_take_apart ( $x ) ) ;
+       $y = coordinate_to_number ( coordinate_take_apart ( $y ) ) ;
+
+       $z = $x ; $x = $y ; $y = $z ;
+       $y = 90 * 3600 - $y ;
+
+       $x /= 100 ;
+       $y /= 100 ;
 
        # Recording min and max
        global $min_x , $min_y , $max_x , $max_y ;
@@ -114,6 +135,7 @@ class geo
                                {
                                $x = trim ( array_shift ( $a ) ) ;
                                $y = trim ( array_shift ( $a ) ) ;
+                               data_to_real ( $x , $y ) ;
                                $ret[] = array ( $x , $y ) ;
                                }
                        }
@@ -211,7 +233,7 @@ class geo
                                {
                                $x = $a[0] ;
                                $y = $a[1] ;
-                               data_to_real ( $x , $y ) ;
+//                             data_to_real ( $x , $y ) ;
                                $this->xsum += $x ;
                                $this->ysum += $y ;
                                $this->count++ ;
@@ -228,6 +250,8 @@ class geo
                $text = $this->get_specs ( "name" , array ( "de" ) ) ;
                if ( $text == "" ) return "" ;
                $text = utf8_decode ( $this->data[$text][0] ) ;
+               $x = floor ( $x ) ;
+               $y = floor ( $y ) ;
                $ret = "<text style='text-anchor:middle;fill-opacity:0.7' x='{$x}' y='{$y}'>{$text}</text>\n" ;
                return $ret ;
                }
@@ -277,11 +301,14 @@ $min_y -= $height / 10 ;
 $max_x += $width / 10 ;
 $max_y += $height / 10 ;
 
+$max_x -= $min_x ;
+$max_y -= $min_y ;
+
 $svg = 
 '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">
-<svg viewBox="' . "{$min_x} {$min_y} {$max_x} {$max_y}" . '"
-     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
+<svg viewBox="' . "{$min_x} {$min_y} {$max_x} {$max_y}" .
+'" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
 '
        . $styles .
 '      <g id="mainlayer">
diff --git a/experiments/geo/geo_functions.php b/experiments/geo/geo_functions.php
new file mode 100644 (file)
index 0000000..ce21687
--- /dev/null
@@ -0,0 +1,65 @@
+<?
+
+# Converts a string like "-123456" into array ( "-12" , "34" , "56" )
+function coordinate_take_apart ( $c )
+       {
+       if ( substr ( $c , 0 , 1 ) == "-" )
+               {
+               $mul = -1 ;
+               $c = substr ( $c , 1 ) ;
+               }
+       else $mul = 1 ;
+       while ( strlen ( $c ) < 6 ) $c .= "0" ;
+       $c = array ( substr ( $c , 0 , 2 ) , substr ( $c , 2 , 2 ) , substr ( $c , 4 , 2 ) ) ;
+       $c[0] *= $mul ;
+       return $c ;
+       }
+
+function coordinate_write ( $a )
+       {
+       if ( !is_array ( $a ) ) $a = number_to_coordinate ( $a ) ;
+       if ( $a[0] < 0 ) $mul = -1 ;
+       else $mul = 1 ;
+       $a[0] *= $mul ;
+       while ( strlen ( $a[0] ) < 2 ) $a[0] = "0" . $a[0] ;
+       while ( strlen ( $a[1] ) < 2 ) $a[1] = "0" . $a[1] ;
+       while ( strlen ( $a[2] ) < 2 ) $a[2] = "0" . $a[2] ;
+       if ( $mul == -1 ) $r = "-" ;
+       else $r = "" ;
+       $r .= $a[0] . $a[1] . $a[2] ;
+       return $r ;
+       }
+
+function coordinate_to_number ( $a )
+       {
+       if ( is_string ( $a ) ) $a = coordinate_take_apart ( $a ) ;
+       if ( $a[0] < 0 ) $mul = -1 ;
+       else $mul = 1 ;
+       return $mul * ( $a[2] + $a[1] * 60 + $a[0] * 3600 * $mul ) ;
+       }
+
+function number_to_coordinate ( $a )
+       {
+       $b = array () ;
+       $b[0] = floor ( $a / 3600 ) ;
+       $rest = $a - $b[0] * 3600 ;
+       $b[1] = floor ( $rest / 60 ) ;
+       $b[2] = floor ( $rest - $b[1] * 60 ) ;
+       return $b ;
+       }
+
+function coord_conversion_params ( $c1 , $c2 , $p1 , $p2 )
+       {
+       $a = ( $c1 - $c2 ) / ( $p1 - $p2 ) ;
+       $b = $c1 - $a * $p1 ;
+       return array ( $a , $b ) ;
+       }
+
+function point_to_coords ( $s , $cx , $cy )
+       {
+       $x = $s[0] * $cx[0] + $cx[1] ;
+       $y = $s[1] * $cy[0] + $cy[1] ;
+       return array ( $x , $y ) ;
+       }
+
+?>
\ No newline at end of file
index d6a398a..b6bbafb 100644 (file)
@@ -1,4 +1,5 @@
 <?
+include_once ( "geo_functions.php" ) ;
 
 print "
 <html>
@@ -6,7 +7,12 @@ print "
 <body>
 " ;
 
-$image = "./Germany_Map.jpg" ;
+# One should only use free maps here, but since this is just a test...
+$image = "http://www.bmwnation.com/members/ausgang/Germany%20Map.jpg" ;
+
+# Corner points of the image above:
+# 361,502 <=> 480900,113500
+# 277,67 <=> 541919,100803
 
 if ( isset ( $_GET['coords'] ) ) $coords = $_GET['coords'] ;
 else $coords = "" ;
@@ -43,8 +49,52 @@ print "
 print "<br/><a href='geomaker.php?coords={$c2}'>Remove last coordinates</a>" ;
 print " | <a href='geomaker.php'>Reset</a>" ;
 
+# Conversion form
+
+if ( isset ( $_POST['convert'] ) )
+       {
+       $p1 = explode ( "," , $_POST['p1'] ) ;
+       $p2 = explode ( "," , $_POST['p2'] ) ;
+       $np1 = explode ( "," , $_POST['np1'] ) ;
+       $np2 = explode ( "," , $_POST['np2'] ) ;
+
+       $np1 = array ( coordinate_to_number ( $np1[0] ) , coordinate_to_number ( $np1[1] ) ) ;
+       $np2 = array ( coordinate_to_number ( $np2[0] ) , coordinate_to_number ( $np2[1] ) ) ;
+
+       $cx = coord_conversion_params ( $np1[1] , $np2[1] , $p1[0] , $p2[0] ) ;
+       $cy = coord_conversion_params ( $np1[0] , $np2[0] , $p1[1] , $p2[1] ) ;
+
+       $t = $_POST['ctext'] ;
+       $coords = "" ;
+       $t = explode ( " " , $t ) ;
+       foreach ( $t AS $s )
+               {
+               $s = explode ( "," , trim ( $s ) ) ;
+               if ( count ( $s ) == 2 )
+                       {
+                       $np = point_to_coords ( $s , $cx , $cy ) ;
+                       $coords[] = coordinate_write($np[1]) . "," . coordinate_write($np[0]) ;
+                       }
+               }
+       $coords = implode ( " " , $coords ) ;
+
+       # For output
+       $p1 = $_POST['p1'] ;
+       $p2 = $_POST['p2'] ;
+       $np1 = $_POST['np1'] ;
+       $np2 = $_POST['np2'] ;
+       }
+else $p1 = $p2 = $np1 = $np2 = "" ;
+
 print "<br/>Coordinates so far:<br>\n" ;
+print "<form method=post><textarea style='width:100%' rows=5 cols=40 name='ctext'>\n" ;
 print str_replace ( ";" , " " , $coords ) ;
+print "</textarea>\n" ;
+print "Conversion : Point <input type='text' name='p1'/ value='{$p1}'> matches coordinates <input type='text' name='np1' value='{$np1}'/><br>" ;
+print "and point <input type='text' name='p2' value='{$p2}'/> matches coordinates <input type='text' name='np2' value='{$np2}'/>" ;
+print " <input type='submit' name='convert' value='Convert'/>" ;
+print "</form>" ;
+
 
 print "
 </body>