ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
1281
1281
EMPFEHLEN
-
10.10.09 21:51 #1
Mitglied Brokat
- Registriert seit
- Feb 2005
- Beiträge
- 457
Soo, etwas verspätet, hatte garnicht mitbekommen, dass das der Contest online war... danke an Matthias dafür nochmal... Und auch für die Google-Geocode-Funktion, die ich im Prinzip übernommen habe...
Meine Berechnung der Route läuft im Wesentlichen so:
Code python:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
def route( start, end, steps = 50 ): # Punkte auf der Erd-Kugel bestimmen v_start = Vector.from_location( start ) v_end = Vector.from_location( end ) # Direkten Verbindungsvektor 'durch' die Erde finden v_dir = v_end - v_start # Stückweise den Verbindungsvektor abgehen points = [] for step in xrange( steps + 1 ): frac = step / float(steps) # Einen Vector vom Mittelpunkt der Erde finden, # der durch den direkten Verbindungsvektor geht # und diesen bis zur Erdoberfläche verlängern. v_current = (v_start + (v_dir * frac)).normalize() # Nun haben wir einen Punkt, den wir in # unsere Zeichnung einfügen können. points.append( (v_current.lo, v_current.la) ) # Ferdisch! return points
Wenn ich das richtig sehe haben damit fast alle, die abgegeben haben, einen anderen Lösungsansatz gewählt...
Komplettes Python Script:
Code python:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
# -*- encoding: utf8 -*- import re import sys import math import urllib def __main__(): # Alle Zeilen von der Standard-Eingabe lesen lines = sys.stdin.readlines() if not lines: return # Alle Zeilen parsen entries = [parse_location_line(line) for line in lines if line.strip()] # SVG erzeugen svg = [ svg_start() ] svg_layer = [] last_loc, last_name = None, None for loc, name in entries: # Kreuz und Name zeichnen svg_layer.append( svg_cross( *loc ) ) svg_layer.append( svg_text( loc.lo - 5, loc.la + 5, name ) ) # Wenn wir schon eine Position hatten, zeichne # die Route ein if last_loc: for points in route( last_loc, loc ): svg.append( svg_polyline( points ) ) # setzte "letzte" Position last_loc, last_name = loc, name # SVG ausgeben svg.append( "".join( svg_layer ) ) svg.append( svg_end() ) print "".join( svg ) # # Berechnet eine Route zwischen 'start' und 'end' # def route( start, end, steps = 50 ): # Punkte auf der Erd-Kugel bestimmen v_start = Vector.from_location( start ) v_end = Vector.from_location( end ) # Direkter Verbindungsvektor 'durch' die Erde finden v_dir = v_end - v_start # Stückweise die Route abgehen points = [] parts = [] last_sgn = 0 for step in xrange( steps + 1 ): frac = step / float(steps) # Einen Vector vom Mittelpunkt der Erde finden, # der durch den direkten Verbindungsvektor geht # und diesen bis zur Erdoberfläche verlängern. v_current = (v_start + (v_dir * frac)).normalize() # Schauen ob wir über einen Rand übertreten haben. Nämlich genau # dann, wenn sich das Vorzeichen der Länge geändert hat, # und der Betrag der länge > 90° ist sgn = -1 if v_current.lo < 0 else 1 if last_sgn and abs( v_current.lo ) > 90 and sgn != last_sgn: points.append( Location(v_current.lo - 360 * sgn, v_current.la) ) parts.append( points ) # Neue Punktmenge beginnen points = [] points.append( Location(v_current.lo + 360 * sgn, v_current.la) ) # last_sgn updaten last_sgn = sgn # Nun haben wir einen Punkt, den wir in # unsere Zeichnung einfügen können. points.append( (v_current.lo, v_current.la) ) # Ferdisch! parts.append( points ) return parts # # Zerlegt eine Zeile der Form "lat lon name" und gibt ein tuple # mit (Location, Name) zurück # def parse_location_line( line ): line = line.strip() if line.startswith( "!" ): name = line[1:].strip() return (ask_mother_google( name ), name) parts = filter( None, line.split( " " ) ) if len( parts ) < 3: raise Exception( "Ungültige Zeile: " + line ) return (Location( parts[1], parts[0] ), " ".join( parts[2:] )) # # Fragt Google nach den Koordinaten für einen gegeben Ort # def ask_mother_google( what ): if what in ask_mother_google.cache: return ask_mother_google.cache[what] # URL zusammensetzten url = "http://maps.google.com/maps/geo?" + urllib.urlencode({ "output": "csv", "q": what }) # Google fragen parts = urllib.urlopen( url ).read().split(",") # Fehlerhaftes Ergebnis? if len( parts ) != 4 or parts[0] != "200": raise IOError( "Der Ort '%s' wurde nicht gefunden!" % what ) # Breitengrad invertieren... Google ist da anderer Meinung als ich ;) result = Location( float(parts[3]), -float(parts[2]) ) ask_mother_google.cache[what] = result return result # Mehrfache Anfragen cachen ask_mother_google.cache = {} # # Wandelt eine Angabe in Grad, Minuten und Sekunden in einen Winkel in # Grad um. # def parse_degree( input ): # Mit regulärem Ausdruck aufteilen match = re.findall( r'([0-9]+)°(?:([0-9]+)\')?(?:([0-9]+)")?([NOSWE])', input ) if not match: raise Exception( "Ungültige Gradangabe: " + input ) g, m, s, cardinal = match[0] # Gradwert berechnen deg = float( g ) if m: deg += float(m) / 60 if s: deg += float(s) / 3600 # Norden oder Westen invertieren if cardinal in 'NW': deg *= -1 return deg # # Ein Tupel welches das erste und zweite Element mittels .lo und .la # zurück gibt # class Location( tuple ): def __new__( cls, lo, la ): if isinstance( lo, str): lo = parse_degree( lo ) if isinstance( la, str): la = parse_degree( la ) return tuple.__new__( cls, (lo, la) ) @property def lo( self ): return self[0] @property def la( self ): return self[1] # # Eine kleine Vektor Klasse, zum einfachen Rechnen mit Vektoren # class Vector( tuple ): def __new__( cls, x, y, z ): return tuple.__new__( cls, (x, y, z) ) def __add__( self, other ): return Vector( *map( lambda a, b: a + b, self, other ) ) def __sub__( self, other ): return Vector( *map( lambda a, b: a - b, self, other ) ) def __mul__( self, scale ): return Vector( *[a * scale for a in self] ) def normalize( self ): return self * (1 / self.length) @property def length( self ): return math.sqrt( sum( [a * a for a in self] ) ) @property def la( self ): return math.degrees( math.asin( self.y ) ) @property def lo( self ): return math.degrees( math.atan2( self.x, self.z ) ) @property def x( self ): return self[0] @property def y( self ): return self[1] @property def z( self ): return self[2] @staticmethod def from_location( loc ): lo = math.radians( loc.lo ) la = math.radians( loc.la ) return Vector( math.sin( lo ) * math.cos( la ), math.sin( la ), math.cos( lo ) * math.cos( la ) ) def svg_start(): return '''<?xml version="1.0" ?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1024" height="512" stroke="red" fill="none" viewBox="-180 -90 360 180" stroke-width="0.3"> <defs> <g id='Kreuz'> <line x1="-1" y1="-1" x2="1" y2="1" /> <line x1="-1" y1="1" x2="1" y2="-1" /> </g> </defs> <!-- <image x="-180" y="-90" width="360" height="180" xlink:href="http://veimages.gsfc.nasa.gov/2430/land_ocean_ice_2048.jpg" /> --> <image x="-180" y="-90" width="360" height="180" xlink:href="world.jpg" /> ''' def svg_cross( x, y ): return "<use xlink:href='#Kreuz' transform='translate(%f %f)' />" % (x, y) def svg_polyline( points ): return "<polyline points='%s' />" % " ".join( [str(c) for p in points for c in p] ) def svg_text( x, y, text ): return "<text x='%f' y='%f' fill='#ff0' stroke='none' font-size='5'>%s</text>" % (x, y, text) def svg_end(): return "</svg>" if __name__ == '__main__': __main__()
-
10.10.09 23:10 #2„Gib einem Menschen einen Fisch, und er wird für einen Tag satt. Lehre ihn Fischen, und er wird ein Leben lang satt.“
“For every complex problem, there is an answer that is short, simple and wrong.”
“Pessimism is safe, but optimism is a lot faster!”
Aktuelles Coding Quiz: #17 - Wörter kreuz und quer
Ähnliche Themen
-
[Quiz#11] OnlyFoo (python)
Von OnlyFoo im Forum ArchivAntworten: 2Letzter Beitrag: 25.10.09, 06:22






Danke jedenfalls dass du noch abgegeben hast!
Login