예제 #1
0
 def test_lawrence_nearby_list(self):
     office = Point(-95.286830, 38.971330)
     stations = find_nearest_weather_stations(office) # will return 5
     self.failUnlessEqual(stations[0].code, u'KLWC')
     self.failUnlessEqual(stations[1].code, u'KFOE')
     self.failUnlessEqual(stations[2].code, u'KTOP')
     self.failUnlessEqual(stations[3].code, u'KIXD')
     self.failUnlessEqual(stations[4].code, u'KOJC')
예제 #2
0
def find_weather_stations(request):
    if request.method == 'POST':
        form = SearchForm(request.POST)
        if form.is_valid():
            location = urllib.quote_plus(form.cleaned_data['location'])
            request_url = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s" % (location, "csv", settings.GOOGLE_MAPS_API_KEY)
            data = urllib.urlopen(request_url).read()
            dlist = data.split(',')
            if dlist[0] == '200':
                point = Point((float(dlist[3]), float(dlist[2])),)
            else:
                point = None

            markers = []

            if point:
                marker = GMarker(point, request.POST['location'])
                marker.add_event(GEvent('click', 'function() { geodjango.map_marker1.openInfoWindowHtml("%s") }' % form.cleaned_data['location']))
                # the gis.maps.google.* stuff doesn't allow for ^^^^^ dynamic naming of these - with multiple points, it will be
                # necessary to for loop through the points with your own counter (start from zero) and that should coincide with
                # the template forloop counter being used - but by all means cross-check that every time to make sure it's right.
                markers.append(marker)

                weather_stations = find_nearest_weather_stations(point, qty=10)

                count = 2
                for station in weather_stations:
                    marker = GMarker(station.point, '%s %s' % (station.code, station.get_name()))
                    marker.add_event(GEvent('click', 'function() { geodjango.map_marker%s.openInfoWindowHtml("%s"); }' % \
                        (count, "%s, (altitude %s ft.) - %s mi @ %s deg""" \
                        % (station.code, floatformat(m_to_ft(station.elevation), 0), floatformat(station.distance.mi, 2), floatformat(rad_to_deg(station.azimuth))))
                    ))
                    markers.append(marker)
                    count = count + 1

                map = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY, markers=markers)

                return render_to_response('locationfinder.html', {
                    'location': form.cleaned_data['location'],
                    'location_point': point,
                    'google': map,
                    'weather_stations': weather_stations,
                })
            else:
                return render_to_response('locationfinder.html', {
                    'location': location,
                })
        else:
            return render_to_response('locationfinder.html', {'form': form,})
    else:
        form = SearchForm()
        return render_to_response('locationfinder.html', {'form': form,})