예제 #1
0
    def tracks_info(self, request, pk):
        result = []
        started_at = request.query_params.get('started_at')
        finished_at = request.query_params.get('finished_at')
        tracks = Track.objects.filter(vehicle=pk,
                                      started_at__gt=started_at,
                                      finished_at__lt=finished_at)
        geolocator = Yandex(api_key='3a9de515-8d7f-49d0-a0eb-24f7d5d13533')

        for track in tracks:
            start_place_coords = GEOSGeometry(track.route[0]).coords[::-1]
            finished_place_coords = GEOSGeometry(track.route[-1]).coords[::-1]
            start_place = geolocator.reverse(start_place_coords,
                                             kind='locality')
            finished_place = geolocator.reverse(finished_place_coords,
                                                kind='locality')

            result.append({
                'started_at':
                track.started_at,
                'finished_at':
                track.finished_at,
                'started_place':
                start_place.address
                if start_place is not None else 'Неизвестное местоположение',
                'finished_place':
                finished_place.address
                if finished_place is not None else 'Неизвестное местоположение'
            })

        return Response(result)
예제 #2
0
def reverse_geolocate(lat, lon, yandex=False, try_all=True):
    data = {}
    if yandex:
        geolocator = Yandex(lang='en_US')
        location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10)
        if location is None or not len(location):
            if try_all:
                return reverse_geolocate(lat, lon, False, False)
            return data
        location = location[0]
        data["country"] = location.address.split(",")[-1].strip()
        try:
            data["city"] = location.address.split(",")[-3].strip()
        except:
            data["city"] = location.address.split(",")[0].strip()
        try:
            data["street"] = " ".join(location.address.split(",")[:-3]).strip()
        except:
            data["street"] = location.address
    else:
        geolocator = Nominatim()
        location = geolocator.reverse(str(lat) + ", " + str(lon), timeout=10)
        if location is None and try_all:
            return reverse_geolocate(lat, lon, True, False)
        data["country"] = location.address.split(",")[-1].strip()
        data["zip"] = None
        # check for zip code
        if location.address.split(",")[-2].strip().replace("-", "").replace(
                "_", "").replace(" ", "").isdigit():
            data["zip"] = location.address.split(",")[-2].strip()
            data["state"] = location.address.split(",")[-3].strip()
            try:
                data["region"] = location.address.split(",")[-4].strip()
                data["city"] = location.address.split(",")[-5].strip()
                data["street"] = " ".join(
                    location.address.strip().split(",")[:-5])
            except:
                data["city"] = location.address.split(",")[-0].strip()
                data["region"] = data["state"]
                data["street"] = data["city"]
        else:
            data["state"] = location.address.split(",")[-2].strip()
            try:
                data["region"] = location.address.split(",")[-3].strip()
                data["city"] = location.address.split(",")[-4].strip()
                data["street"] = " ".join(
                    location.address.strip().split(",")[:-4])
            except:
                data["city"] = location.address.split(",")[-0].strip()
                data["street"] = location.address

    data["latitude"] = location.latitude
    data["longitude"] = location.longitude
    data["country"] = location.address.split(",")[-1].strip()
    data["address"] = location.address
    return data
예제 #3
0
def Index(request):
    # #
    # from django.contrib.sites.shortcuts import get_current_site
    # current_site = get_current_site(request)
    # for city in City.objects.all():
    #     with open('links.txt', 'a', encoding='utf-8') as file:
    #         file.write('https://'+current_site.domain+city.get_absolute_url()+'\n')

    # for city in City.objects.all():
    #         for spec in Specialization.objects.all():
    #             with open('links.txt', 'a', encoding='utf-8') as file:
    #                 file.write('https://'+current_site.domain+reverse('mainapp:LawyersBySpecialization', args=[city.slug, spec.slug])+'\n')

    # for lawyer in Lawyer.objects.all().order_by('id'):
    #     with open('links.txt', 'a', encoding='utf-8') as file:
    #         file.write('https://'+current_site.domain+lawyer.get_absolute_url()+'\n')
    # #
    if request.GET.get('lat'):
        from django.conf import settings
        from geopy.geocoders import Yandex
        from django.http import HttpResponse, JsonResponse
        import json
        geo = Yandex(api_key=settings.YANDEX_MAP_TOKEN)
        lat = request.GET.__getitem__('lat')
        lng = request.GET.__getitem__('lng')
        city_name = geo.reverse((lat, lng),
                                exactly_one=True,
                                timeout=5,
                                kind="locality").address.split(', ')[0]
        try:
            city_slug = City.objects.get(name=city_name).slug
            request.session['city'] = city_slug
        except:
            pass
        return HttpResponse(json.dumps(
            {
                'city_slug': city_slug,
                'city_name': city_name
            },
            ensure_ascii=False),
                            content_type='application/json')
    else:
        # specializations = Specialization.objects.annotate(num_subspecs = Count('related')).filter(num_subspecs__gt=0).order_by('num_subspecs').reverse()
        return render(request, 'index.html')