Beispiel #1
0
def geocode(address, api_key=None):

    api_key = getattr(settings, 'WORKON_GOOGLE_API_KEY', api_key)
    if api_key and api_key.strip() == "":
        api_key = None

    api = Geocoding(api_key=api_key)
    if address.strip() != '':
        original_address = address.strip()
        try:

            latlng = latlngreg.search(original_address)
            if latlng:
                results = api.reverse(float(latlng.group(1)),
                                      float(latlng.group(2)))
            else:
                results = api.geocode(original_address)

            if len(results):
                results[0]['lng'] = results[0].get('geometry', {}).get(
                    'location', {}).get('lng', None)
                results[0]['lat'] = results[0].get('geometry', {}).get(
                    'location', {}).get('lat', None)
                return results[0]

        except NoResults, e:
            return {'error': 'NoResults', 'message': str(e.message)}
        except GmapException, e:
            return {'error': 'GmapException', 'message': str(e.message)}
Beispiel #2
0
def geocode(address, api_key=None):

    api_key = getattr(settings, 'WORKON_GOOGLE_API_KEY', api_key)
    if api_key and api_key.strip() == "":
        api_key = None

    api = Geocoding(api_key=api_key)
    if address.strip() != '':
        original_address = address.strip()
        try:

            latlng = latlngreg.search(original_address)
            if latlng:
                results = api.reverse(float(latlng.group(1)), float(latlng.group(2)))
            else:
                results = api.geocode(original_address)

            if len(results):
                results[0]['lng'] = results[0].get('geometry', {}).get('location', {}).get('lng', None)
                results[0]['lat'] = results[0].get('geometry', {}).get('location', {}).get('lat', None)
                return results[0]

        except NoResults, e:
            return { 'error': 'NoResults', 'message': str(e.message)  }
        except GmapException, e:
            return { 'error': 'GmapException', 'message': str(e.message) }
Beispiel #3
0
def get_zip(loc):
    try:
        latitude = loc[0].get('data-latitude')
        longitude = loc[0].get('data-longitude')
        google_api = Geocoding('AIzaSyBkWTcFg-kawvHmt1MryKvMcpmsNmrGWPU')
        map_loc = google_api.reverse(float(latitude), float(longitude))
        items = map_loc[0]['address_components']
        result = [item['long_name'] for item in items if item['types'][0] == 'postal_code']
        code = 'USA-' + result[0]
        return code
    except IndexError:
        return None
Beispiel #4
0
def get_zip(loc):
    try:
        latitude = loc[0].get('data-latitude')
        longitude = loc[0].get('data-longitude')
        google_api = Geocoding('AIzaSyBkWTcFg-kawvHmt1MryKvMcpmsNmrGWPU')
        map_loc = google_api.reverse(float(latitude), float(longitude))
        items = map_loc[0]['address_components']
        result = [
            item['long_name'] for item in items
            if item['types'][0] == 'postal_code'
        ]
        code = 'USA-' + result[0]
        return code
    except IndexError:
        return None
Beispiel #5
0
 def test_json_like_geocode_call(self):
     gmaps_api = Geocoding(
         **{
             'sensor': True,
             'use_https': True,
             'api_key': u'{}'.format(settings.GMAPS_API_KEY)}
     )
     california = GmapsItem.objects.get(short_name="CA")
     california_response = california.get_response_json()
     geocode_response = gmaps_api.reverse(
         float(california.lat), float(california.lng),
         **{'language': 'en',
            'result_type': ['administrative_area_level_1']}
     )
     self.assertEqual(california_response, json.dumps(geocode_response[0]))
Beispiel #6
0
 def test_json_like_geocode_call(self):
     gmaps_api = Geocoding(
         **{
             'sensor': True,
             'use_https': True,
             'api_key': u'{}'.format(settings.GMAPS_API_KEY)
         })
     california = GmapsItem.objects.get(short_name="CA")
     california_response = california.get_response_json()
     geocode_response = gmaps_api.reverse(
         float(california.lat), float(california.lng), **{
             'language': 'en',
             'result_type': ['administrative_area_level_1']
         })
     self.assertEqual(california_response, json.dumps(geocode_response[0]))
Beispiel #7
0
@route('/todo')
def todo_list():
    conn = sqlite3.connect('sessions.db')
    c = conn.cursor()
    c.execute("SELECT id, dt, data FROM DATA")
    result = c.fetchall()
    c.close()
    output = template('templates/maketable', rows = result)
    return output

@route('/map')
def todo_list():
    conn = sqlite3.connect('sessions.db')
    c = conn.cursor()
    c.execute("SELECT id, dt, data FROM DATA")
    result = c.fetchall()
    c.close()
    output = template('templates/maps', rows = result)
    return output
api = Geocoding()

api.geocode("somwhere")

print(api.reverse(51.123, 21.123))
print(api.base)
run(port = 8989, host = '192.168.0.156' )



Beispiel #8
0
from gmaps import Geocoding
api = Geocoding()

api.geocode("somwhere")
api.reverse(51.123, 21.123)
Beispiel #9
0
def route_planing(request, id, ide):

	def new_last_point(event, point, lat, lng):
		waypoint_event = Waypoint_event()
		waypoint_event.point = point
		waypoint_event.name = point
		waypoint_event.point_lat = lat
		waypoint_event.point_lng = lng
		waypoint_event.user = request.user
		event_end_point = event.end_point()
		waypoint_event.order = event_end_point.order
		waypoint_event.number = event_end_point.number
		waypoint_event.event = event
		waypoint_event.save()
		event_end_point.order += 1
		event_end_point.number += 1
		event_end_point.save()

	event = Events.objects.filter(id = id, ide = ide)[0]

	form1a = Events3aForm(instance = event)
	form2a = Events4aForm(instance = event)
	form3a = Events5aForm(instance = event)
	form1b = Events3bForm(instance = event)
	form2b = Events4bForm(instance = event)
	form3b = Events5bForm(instance = event)

	if request.method == "POST":

		if 'save_route' in request.POST:
			form1a = Events3aForm(request.POST, instance = event)
			form2a = Events4aForm(request.POST, instance = event)
			form3a = Events5aForm(request.POST, instance = event)
			form1b = Events3bForm(request.POST, instance = event)
			form2b = Events4bForm(request.POST, instance = event)
			form3b = Events5bForm(request.POST, instance = event)
			if form1a.is_valid() and form2a.is_valid() and form3a.is_valid() and form1b.is_valid() and form2b.is_valid() and form3b.is_valid():
				form1a.save()
				form2a.save()
				form3a.save()
				form1b.save()
				form2b.save()
				form3b.save()
			return HttpResponseRedirect( reverse('route_details', args=(event.id, event.ide)) )

		if 'new_point_form' in request.POST:
			new_last_point(Events.objects.filter(id = id, ide = ide)[0], request.POST.get('point'), request.POST.get('point_lat'), request.POST.get('point_lng'))
			return HttpResponse(json.dumps({"data": "true"}),content_type="application/json")

		if 'new_point_click' in request.POST:
			event = Events.objects.filter(id = id, ide = ide)[0]
			api = Geocoding()
			nombre = ''
			for direccion in api.reverse( float(request.POST.get('lat')), float(request.POST.get('lng')))[0]['address_components']:
				entra = True
				nombre += direccion['long_name']
				nombre += ', '
			if entra:
				nombre = nombre[:-2]
			new_last_point(Events.objects.filter(id = id, ide = ide)[0], nombre, request.POST.get('lat'), request.POST.get('lng'))
			return HttpResponse(json.dumps({"data": "true"}),content_type="application/json")
		
		if 'del_waypoint' in request.POST:
			event = Events.objects.filter(id = id, ide = ide)[0]
			Waypoint_event.objects.filter(id=request.POST.get('del_waypoint'))[0].delete()
			waypoints = Waypoint_event.objects.filter(event=event).order_by('order')
			order = 1
			number = 1
			for obj in waypoints:
				obj.order = order
				obj.number = number
				obj.save()
				order += 1
				number += 1
			return HttpResponse(json.dumps({"data": "true"}),content_type="application/json")

		if 'order' in request.POST:
			data  = json.loads(request.POST.get('order'))
			order = 1
			number = 1
			for obj in data:
				waypoint = Waypoint_event.objects.filter(id=obj)[0]
				waypoint.order = order
				waypoint.number = number
				waypoint.save()
				order += 1
				number += 1
			return HttpResponse(json.dumps({"data": "true"}),content_type="application/json")
		
	event = Events.objects.filter(id = id, ide = ide)[0]
	waypoints = Waypoint_event.objects.filter(event=event).order_by('order')
	
	return render(request, 'app/route_planing.html',locals())