Exemplo n.º 1
0
def update_site_covariates(site):
    site_pk = site.pk

    point = site.location
    north_pole = (90, 0)
    south_pole = (-90, 0)
    existing_covariates = set(site.covariates.all().values_list("name",
                                                                flat=True))
    supported_covariates = set([c for c, _ in Covariate.SUPPORTED_COVARIATES])
    coral_atlas = CoralAtlasCovariate()

    if ((site_pk and Site.objects.get(pk=site_pk).location == point
         and not supported_covariates.difference(existing_covariates))
            or geopy_distance(
                (point.y, point.x), north_pole).km < coral_atlas.radius
            or geopy_distance(
                (point.y, point.x), south_pole).km < coral_atlas.radius):
        return

    results = coral_atlas.fetch([(point.x, point.y)])

    if not results:
        return

    result = results[0]

    data_date = result["date"]
    requested_date = result["requested_date"]
    aca_covariates = result.get("covariates") or dict()
    aca_benthic = aca_covariates.get("aca_benthic") or []
    aca_geomorphic = aca_covariates.get("aca_geomorphic") or []

    aca_benthic_covariate = Covariate.objects.get_or_none(
        name="aca_benthic", site_id=site_pk) or Covariate(name="aca_benthic",
                                                          site=site)
    aca_benthic_covariate.display = "Alan Coral Atlas Benthic"
    aca_benthic_covariate.datestamp = data_date
    aca_benthic_covariate.requested_datestamp = requested_date
    aca_benthic_covariate.value = aca_benthic
    aca_benthic_covariate.save()

    aca_geomorphic_covariate = Covariate.objects.get_or_none(
        name="aca_geomorphic", site=site) or Covariate(name="aca_geomorphic",
                                                       site=site)
    aca_geomorphic_covariate.display = "Alan Coral Atlas Geomorphic"
    aca_geomorphic_covariate.datestamp = data_date
    aca_geomorphic_covariate.requested_datestamp = requested_date
    aca_geomorphic_covariate.value = aca_geomorphic
    aca_geomorphic_covariate.save()
Exemplo n.º 2
0
 def resolve_tweet(self, tweet):
     # The Twitter API allows tweet['coordinates'] to both be absent
     # and None, such that the key exists but has a None value.
     # "tweet.get('coordinates', {})" would return None in the latter
     # case, with None.get() in turn causing an AttributeError. (None
     # or {}), on the other hand, is {}, and {}.get() is okay.
     tweet_coordinates = (tweet.get('coordinates') or {}).get('coordinates')
     if not tweet_coordinates:
         return None
     tweet_coordinates = Point(longitude=tweet_coordinates[0],
                               latitude=tweet_coordinates[1])
     closest_candidate = None
     closest_distance = float('inf')
     for cell in self._cells_for(tweet_coordinates.latitude,
                                 tweet_coordinates.longitude):
         for candidate in self.location_map[cell]:
             candidate_coordinates = Point(
                 candidate.latitude, candidate.longitude)
             distance = geopy_distance(
                 tweet_coordinates, candidate_coordinates).miles
             if closest_distance > distance:
                 closest_candidate = candidate
                 closest_distance = distance
     if closest_distance < self.max_distance:
         return (False, closest_candidate)
     return None
Exemplo n.º 3
0
 def get_customers_within_miles(self, x=5):
     from geopy.distance import distance as geopy_distance
     return [
         i.pk for i in Customer.objects.all()
         if i.location and self.location and
         geopy_distance(i.location.location, self.location.location).mi <= x
     ]
Exemplo n.º 4
0
def findInteractionDataFromShapes(sourceShape, sourceCollarDataPoint, distance=settings.DEFAULT_INTERACTION_DISTANCE, timedelta=None, starttime=None, endtime=None, interactiongroup=None):
    """
    findInteractionDataFromShapes does the same as the above function for animals but with shapes.
    """
    list_of_data_points_to_search = CollarData.objects.all()
    if starttime:
        if endtime:
            list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__range=(starttime, endtime))
        else:
            list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__gte=starttime)
    elif endtime:
        list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__lte=endtime)
    if distance:
        list_of_data_points_to_search = list_of_data_points_to_search.filter(LOCATION__distance_lte=(sourceShape.shape, D(km=distance)))
    if timedelta:
        real_time_delta = datetime.timedelta(seconds=timedelta)
        start_delta = sourceCollarDataPoint.GMT_DATETIME - real_time_delta
        end_delta = sourceCollarDataPoint.GMT_DATETIME + real_time_delta
        list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__range=(start_delta, end_delta))
    for match in list_of_data_points_to_search:
        shape_interaction_to_add = ShapeInteraction()
        shape_interaction_to_add.source_shapes = sourceShape
        shape_interaction_to_add.destination_animal_data_point = match
        shape_interaction_to_add.distance = geopy_distance(sourceShape.shape,match.LOCATION).kilometers
        shape_interaction_to_add.interaction_group = interactiongroup
        shape_interaction_to_add.date_time = match.GMT_DATETIME
        try:
            shape_interaction_to_add.clean()
            shape_interaction_to_add.save()
        except ValidationError:
            print "The entry already exists"
    zip_content = ContentFile(exportShape())
    interactiongroup.zip_file.save("sampleFile.zip",zip_content)
    interactiongroup.save()
Exemplo n.º 5
0
def find_place_from_info(info, provider):
    from .venue_providers import venue_providers

    venue_provider = venue_providers[provider]
    if not info.get('geo'):
        if not info.get('address'):
            print('No address found.')
            return
        address = info['address']
        point, formatted_address = geocode(address)
        if not point:
            print('Geocoding failed.', address)
            return
        info['geo'] = point
        info['address'] = formatted_address

    place = venue_provider.match_place(
        [info['geo'].coords[1], info['geo'].coords[0]], info['name'])
    if not place:
        print('No match found.')
        return None
    place_point = Point(place['lng'], place['lat'])
    distance = geopy_distance(place_point, info['geo'])
    if distance.meters > 100:
        print('Matches too far away.')
        return None
    place['geo'] = place_point
    return place
Exemplo n.º 6
0
 def resolve_tweet(self, tweet):
     # The Twitter API allows tweet['coordinates'] to both be absent
     # and None, such that the key exists but has a None value.
     # "tweet.get('coordinates', {})" would return None in the latter
     # case, with None.get() in turn causing an AttributeError. (None
     # or {}), on the other hand, is {}, and {}.get() is okay.
     tweet_coordinates = (tweet.get('coordinates') or {}).get('coordinates')
     if not tweet_coordinates:
         return None
     tweet_coordinates = Point(longitude=tweet_coordinates[0],
                               latitude=tweet_coordinates[1])
     closest_candidate = None
     closest_distance = float('inf')
     for cell in self._cells_for(tweet_coordinates.latitude,
                                 tweet_coordinates.longitude):
         for candidate in self.location_map[cell]:
             candidate_coordinates = Point(candidate.latitude,
                                           candidate.longitude)
             distance = geopy_distance(tweet_coordinates,
                                       candidate_coordinates).miles
             if closest_distance > distance:
                 closest_candidate = candidate
                 closest_distance = distance
     if closest_distance < self.max_distance:
         return (False, closest_candidate)
     return None
Exemplo n.º 7
0
	def get(self,request):
		latitude = self.request.query_params.get('latitude')
		longitude = self.request.query_params.get('longitude')
		radius = self.request.query_params.get('radius')
		point1 = Point(float(latitude), float(longitude))

		count = 0
		avg = 0
		std = 0
		values = []
		for res in Restaurants.objects.raw('select * from melp_res_restaurants'):
			point2 = Point(res.lat, res.lng)
			result = geopy_distance(point1,point2).meters
			if int(result) <= int(radius):
				count = count + 1;
				avg = avg + res.rating
				values.append(res.rating)
		if count > 0:
			avg = avg/count
			addition = 0
			for val in values:
				addition = addition + abs(val-avg)**2
			std = math.sqrt(addition/count)

		return Response({
			"count": count,
			"avg": avg,
			"std": std,
		})
Exemplo n.º 8
0
 def length(self):
     '''Determine the length of the route.'''
     points = (waypoint.point
               for waypoint in self.waypoint_set.orderby('time'))
     meters = sum(
         geopy_distance(a, b).meters for (a, b) in pairwise(points))
     return Distance(m=meters)
Exemplo n.º 9
0
def get_distance(request):
    if request.method == "GET":
        user = request.user
        serialized_user = serializers.serialize('json', [ user ])
        user_latitude = request.GET.get('user_latitude')
        user_longitude = request.GET.get('user_longitude')

        user_position = (user_latitude, user_longitude)

        # fixed_position = (41.8781, 87.6298)
        fixed_position = (-1.3034531999999999, 36.7927116)
        #fixed_position = (-1.2828671999999999, 36.831232)


        distance = geopy_distance(user_position, fixed_position)
        
        my_user ={
            "first_name":user.first_name,
            "last_name":user.last_name
        } 
        dist = distance.meters
        context = {
            "distance":dist,
            "user":serialized_user,
            "user_data":my_user
        }
        print(dist)
      

        return JsonResponse(context,safe=False)
Exemplo n.º 10
0
    def run(self):
        self.logging.info("Running Gps..")

        nm2km = 1.852
        idx = 0

        self.current_position = self.vessel["start_point"]
        self.next_waypoint = self.vessel["waypoints"][idx]

        while True:
            # Coordinates
            lat1 = self.current_position["lat"]
            lon1 = self.current_position["lon"]

            lat2 = self.next_waypoint["lat"]
            lon2 = self.next_waypoint["lon"]

            kts = self.current_position["kts"]

            # Nm travelled over the last random_sleep
            distance = (kts / 3600) * self.random_sleep
            bearing = self.get_bearing(lat1, lon1, lat2, lon2)

            origin = geopy.Point(lat1, lon1)
            destination = geodesic(kilometers=(distance * nm2km)).destination(
                origin, bearing)

            msg = self.gll(destination.latitude, destination.longitude)
            self.logging.info("{}{}".format(self.log_prefix, msg))
            self.mqtt_publisher.publish(self.vessel["id"], msg)

            msg = self.hdt(bearing)
            self.logging.info("{}{}".format(self.log_prefix, msg))
            self.mqtt_publisher.publish(self.vessel["id"], msg)

            msg = self.vhw(bearing, kts)
            self.logging.info("{}{}".format(self.log_prefix, msg))
            self.mqtt_publisher.publish(self.vessel["id"], msg)

            self.current_position["lat"] = destination.latitude
            self.current_position["lon"] = destination.longitude

            # Work out how close to waypoint we are.
            distanceToWaypoint = geopy_distance(
                geopy.Point(self.current_position["lat"],
                            self.current_position["lon"]),
                geopy.Point(lat2, lon2)).miles
            if distanceToWaypoint < 1:
                if idx == (len(self.vessel["waypoints"]) - 1):
                    idx = 0
                else:
                    idx += 1

                self.current_position = self.next_waypoint
                self.next_waypoint = self.vessel["waypoints"][idx]

            # print("DISTANCE", distanceToWaypoint)

            time.sleep(self.random_sleep)
Exemplo n.º 11
0
def get_restaurants(longitude, latitude, categories):
    '''
    Returns objects at given point that satisfy set of categories, 
    or all of them if categories is empty.
    input:
        str longitude
        str latitude
        list categories
    output:
        list of dicts
    '''
    currentPoint = geos.GEOSGeometry('POINT(%s %s)' %(longitude, latitude))
    distance_m = 15000
    list_of_cats = []
    for c in categories:
        list_of_cats.append(models.Category.objects.get(name=c))
    if list_of_cats:
        restaurants = models.Restaurant.gis.filter(
            location__distance_lte=(currentPoint, distance_m), 
            categories__in=list_of_cats
            )
    else:
        restaurants = models.Restaurant.gis.filter(location__distance_lte=(currentPoint, distance_m))
    
    # seems that this thing doesn't actually order objects by distance
    # btw at this step there is no distance property in objects or rows in table
    # restaurants = restaurants.distance(currentPoint).order_by('distance')

    # String based JSON
    data = serializers.serialize('json', restaurants)
    # Actual JSON object to be edited
    data = json.loads(data)

    # if venue has multiple categories and some of them
    # are in list_of_cats than venue will appear in data that some times
    # so we will uniqify venues in data
    if len(list_of_cats) > 1:
        data = {v['pk']:v for v in data}.values()

    for restaurant in data:
        d = geopy_distance(currentPoint, restaurant['fields']['location']).kilometers
        restaurant['fields']['distance'] = round(d, 1)

        # Fancy splitting on POINT(lon, lat)
        lng = restaurant['fields']['location'].split()[1][1:]
        lat = restaurant['fields']['location'].split()[2][:-1]

        del restaurant['fields']['location']
        restaurant['fields']['lng'] = lng
        restaurant['fields']['lat'] = lat

        # Replace category ids with names
        cat_names = []
        for cat_id in restaurant['fields']['categories']:
            cat = models.Category.objects.get(id=cat_id)
            cat_names.append(cat.name)
        restaurant['fields']['categories'] = cat_names

    return data
Exemplo n.º 12
0
 def get_active_customers_miles(self, x=5, filter_pks=[]):
     from geopy.distance import distance as geopy_distance
     if len(filter_pks) > 0:
         return [
             i.pk
             for i in Customer.objects.exclude(pk__in=filter_pks).filter(
                 verified=True, active=True, offer_count=0)
             if i.location and self.location and geopy_distance(
                 i.location.location, self.location.location).mi <= x
         ]
     else:
         return [
             i.pk for i in Customer.objects.filter(
                 verified=True, active=True, offer_count=0)
             if i.location and self.location and geopy_distance(
                 i.location.location, self.location.location).mi <= x
         ]
Exemplo n.º 13
0
    def get_center_coords(cls, min_lat, min_lng, max_lat, max_lng):
        approx_distance = distances.geographic_distance(
                min_lat, min_lng, max_lat, max_lng,)
        
        d = geopy_distance(kilometers=approx_distance / 2.0)
        center = d.destination(GeoPyPoint(max_lat, max_lng), 225)

        return (center.latitude, center.longitude,)
Exemplo n.º 14
0
    def get_center_coords(cls, min_lat, min_lng, max_lat, max_lng):
        approx_distance = distances.geographic_distance(
                min_lat, min_lng, max_lat, max_lng,)

        d = geopy_distance(kilometers=approx_distance / 2.0)
        center = d.destination(GeoPyPoint(max_lat, max_lng), 225)

        return (center.latitude, center.longitude,)
Exemplo n.º 15
0
	def count_merchants_within_miles(self, x=5):
		from geopy.distance import distance as geopy_distance
		count = 0
		for i in Merchant.objects.all() :
			if i.location and self.location:
				if geopy_distance(i.location.location,self.location.location).mi<=x:
					count = count + 1
		return count
Exemplo n.º 16
0
	def count_offers_within_miles (self, x=5):
		from offer.models import Offer
		from geopy.distance import distance as geopy_distance
		count = 0
		for i in Offer.objects.filter(expired_time__gt=datetime.now()):
			if self.location and i.merchant.location:
				if geopy_distance(self.location.location,i.merchant.location.location).mi<=x:
					count = count + 1
		return count 
Exemplo n.º 17
0
 def count_merchants_within_miles(self, x=5):
     from geopy.distance import distance as geopy_distance
     count = 0
     for i in Merchant.objects.all():
         if i.location and self.location:
             if geopy_distance(i.location.location,
                               self.location.location).mi <= x:
                 count = count + 1
     return count
Exemplo n.º 18
0
def get_masjids(longitude, latitude, categories):
     
    '''
    Returns objects at given point that satisfy set of categories,
    or all of them if categories is empty.
    input:
        str longitude
        str latitude
        list categories
    output:
        list of dicts
    '''
    currentPoint =Point(float(longitude), float(latitude)) #geos.GEOSGeometry('POINT(%s %s)' % (longitude, latitude))
    distance_m = 15000
    list_of_cats = []
    for c in categories:
        list_of_cats.append(Cuisine.objects.get(name=c))
    if list_of_cats:
        masjids = Masjid.gis.filter(
            location__distance_lte=(currentPoint, distance_m),
            categories__in=list_of_cats
        )
    else:
        masjids = Masjid.gis.filter(location__distance_lte=(currentPoint, distance_m))

    # String based JSON
    data = serializers.serialize('json', masjids)
    # Actual JSON object to be edited
    data = json.loads(data)

    # if venue has multiple categories and some of them
    # are in list_of_cats than venue will appear in data that some times
    # so we will uniqify venues in data
    if len(list_of_cats) > 1:
        data = {v['pk']: v for v in data}.values()

    for masjid in data:
        d = geopy_distance(currentPoint, masjid['fields']['location']).kilometers
        masjid['fields']['distance'] = round(d, 1)

        # Fancy splitting on POINT(lon, lat)
        lng = masjid['fields']['location'].split()[1][1:]
        lat = masjid['fields']['location'].split()[2][:-1]

        del masjid['fields']['location']
        masjid['fields']['lng'] = lng
        masjid['fields']['lat'] = lat

        # Replace category ids with names
        
        cat_names = []
        for cat in Sect.objects.filter(id=masjid['fields']['sect']):
            cat_names.append(cat.name)
        masjid['fields']['sects'] = cat_names

    return data
Exemplo n.º 19
0
 def get_merchants_within_miles(self, x=5):
     from geopy.distance import distance as geopy_distance
     #print self.address_1 + " " + self.zipcode.code, self.location.location.x, self.location.location.y
     #for i in Merchant.objects.all():
     #print i , i.location.location.x, i.location.location.y, geopy_distance(i.location.location,self.location.location).mi
     return [
         i.pk for i in Merchant.objects.all()
         if i.location and self.location and
         geopy_distance(i.location.location, self.location.location).mi <= x
     ]
Exemplo n.º 20
0
 def count_offers_within_miles(self, x=5):
     from offer.models import Offer
     from geopy.distance import distance as geopy_distance
     count = 0
     for i in Offer.objects.filter(expired_time__gt=datetime.now()):
         if self.location and i.merchant.location:
             if geopy_distance(self.location.location,
                               i.merchant.location.location).mi <= x:
                 count = count + 1
     return count
Exemplo n.º 21
0
def get_masjids(longitude, latitude, categories):
    '''
    Returns objects at given point that satisfy set of categories, 
    or all of them if categories is empty.
    input:
        str longitude
        str latitude
        list categories
    output:
        list of dicts
    '''
    currentPoint = geos.GEOSGeometry('POINT(%s %s)' %(longitude, latitude))
    distance_m = 15000
    list_of_cats = []
    for c in categories:
        list_of_cats.append(models.Category.objects.get(name=c))
    if list_of_cats:
        masjids = models.Masjid.gis.filter(
            location__distance_lte=(currentPoint, distance_m), 
            categories__in=list_of_cats
            )
    else:
        masjids = models.Masjid.gis.filter(location__distance_lte=(currentPoint, distance_m))

    # String based JSON
    data = serializers.serialize('json', masjids)
    # Actual JSON object to be edited
    data = json.loads(data)

    # if venue has multiple categories and some of them
    # are in list_of_cats than venue will appear in data that some times
    # so we will uniqify venues in data
    if len(list_of_cats) > 1:
        data = {v['pk']:v for v in data}.values()

    for masjid in data:
        d = geopy_distance(currentPoint, masjid['fields']['location']).kilometers
        masjid['fields']['distance'] = round(d, 1)

        # Fancy splitting on POINT(lon, lat)
        lng = masjid['fields']['location'].split()[1][1:]
        lat = masjid['fields']['location'].split()[2][:-1]

        del masjid['fields']['location']
        masjid['fields']['lng'] = lng
        masjid['fields']['lat'] = lat

        # Replace category ids with names
        cat_names = []
        for cat_id in masjid['fields']['categories']:
            cat = models.Category.objects.get(id=cat_id)
            cat_names.append(cat.name)
        masjid['fields']['categories'] = cat_names

    return data
Exemplo n.º 22
0
def getClosestStation(dataPoint):
    """
    getClosestStation will call the above scrape stations method to update the
    database first. After this has been completed it will get the location of
    its data point and compare that against all weather stations. Once the
    closest data point has been found it will return the station and the data
    point.
    """
    all_stations = Station.objects.all()
    distance_to_station = geopy_distance(dataPoint.LOCATION, all_stations[0].LOCATION).kilometers
    the_closest_station = all_stations[0]
    for station in all_stations:
        distance = geopy_distance(dataPoint.LOCATION, station.LOCATION).kilometers
        if distance < distance_to_station:
            distance_to_station = distance
            the_closest_station = station
    print "The closest station was: " + str(the_closest_station)
    print "It was: " + str(distance_to_station) + " kilometers away from the collar data point."
    station.distanceToPoint = distance_to_station
    return station
def add_calculated_fields(*, current_item: Dict[str, Any], initial_status,
                          current_status, position_list, lap_list, total,
                          charging_process_list, forecast,
                          configuration: Configuration,
                          current_item_index: Optional[int],
                          now_dt: pendulum.DateTime):
    """
    Add hardcoded calculated fields into current_item
    Note the prototype is the same for all calculated functions even if all inputs are not used

    :param current_item:
    :param initial_status:
    :param current_status:
    :param position_list:
    :param lap_list:
    :param total:
    :param charging_process_list:
    :param forecast:
    :param configuration:
    :param current_item_index:
    :param now_dt: time to calculate data for.
    :return:
    """
    initial_odo = initial_status[
        'odometer'] if 'odometer' in initial_status else None
    current_odo = current_item[
        'odometer'] if 'odometer' in current_item else None
    start_time = configuration.start_time
    end_time = configuration.start_time.add(hours=configuration.hours)

    current_item[
        'distance'] = current_odo - initial_odo if current_odo is not None and initial_odo is not None else None

    current_item['air_distance'] = geopy_distance(
        (configuration.start_latitude, configuration.start_longitude),
        (current_item['latitude'], current_item['longitude'])
        ).km if 'latitude' in current_item and current_item['latitude'] is not None \
                and 'longitude' in current_item and current_item['longitude'] is not None else None

    current_item['start_time'] = start_time
    current_item['end_time'] = end_time

    current_item['time_since_start'] = pendulum.period(start_time, now_dt, True) \
        if now_dt >= start_time else pendulum.period(now_dt, now_dt, True)
    current_item['time_to_end'] = pendulum.period(now_dt, end_time, True) \
        if now_dt <= end_time else pendulum.period(now_dt, now_dt, True)

    current_item['slow_data_age'] = pendulum.period(current_item['slow_data_date'], now_dt) \
        if current_item and 'slow_data_date' in current_item and current_item['slow_data_date'] else None
    current_item['fast_data_age'] = pendulum.period(current_item['fast_data_date'], now_dt) \
        if current_item and 'fast_data_date' in current_item and current_item['fast_data_date'] else None

    current_item['lap_number'] = lap_list[-1]['lap_id'] if lap_list else None
Exemplo n.º 24
0
def distance(*points):
    '''
    Find the geodesic distance between two or more points.

    If more than two points are specified, the points are assumed
    to be on a route. The total length of this route is
    calculated.

    Returns a django.contrib.gis.measure.Distance object.
    '''
    meters = sum(geopy_distance(a, b).meters for (a, b) in pairwise(points))
    return Distance(m=meters)
Exemplo n.º 25
0
def calculate_distance(user_id1, user_id2, units='miles'):
    """
    calculate distance
    https://docs.djangoproject.com/en/1.8/ref/contrib/gis/measure/#supported-units
    km	Kilometre, Kilometer
    mi	Mile
    m	Meter, Metre
    """
    g = GeoIP()
    distance = [10000, 'miles']

    try:
        user1_location = UserLocation.objects.filter(
            user_id=user_id1).order_by('-timestamp')[0]
        user1_point = user1_location.geometry
    except IndexError:
        try:
            user_id1_ip = str(UserIPAddress.objects.get(user_id=user_id1).ip)
            point = g.geos(user_id1_ip)
            if not point:
                return distance
            user1_point = GEOSGeometry(point)
        except UserIPAddress.DoesNotExist:
            return distance

    try:
        user2_location = UserLocation.objects.filter(
            user_id=user_id2).order_by('-timestamp')[0]
        user2_point = user2_location.geometry
    except IndexError:
        try:
            user_id2_ip = str(UserIPAddress.objects.get(user_id=user_id2).ip)
            point = g.geos(user_id2_ip)
            if not point:
                return distance
            user2_point = GEOSGeometry(point)
        except UserIPAddress.DoesNotExist:
            return distance
    try:
        _units = FilterState.objects.filter(user_id=user_id1)[0].distance_unit
        if _units in ('miles', 'km'):
            units = _units
    except IndexError:
        pass

    distance = geopy_distance(user1_point, user2_point)
    if getattr(distance, units) < 1.0:
        if getattr(distance, 'm') <= 10.0:
            return [10, 'meters']
        else:
            return [int(getattr(distance, 'm')), 'meters']
    else:
        return [int(getattr(distance, units)), units]
Exemplo n.º 26
0
    def is_forecast_location_accurate(self, forecast):
        if not forecast.latitude or not forecast.longitude:
            return False

        location_service = LocationService(user = self.__user)
        current_location = location_service.get_current_location()
        current_coords = (current_location.latitude, current_location.longitude)

        forecast_coords = (forecast.latitude, forecast.longitude)
        distance = geopy_distance(current_coords, forecast_coords)
        if distance.km <= 50:
            return True
        else:
            return False
Exemplo n.º 27
0
    def get_bounds(cls, center, distance):
        """
        Returns a Bounds object based on a center point and a distance.
        """

        d = geopy_distance(kilometers=distance)
        point = GeoPyPoint(center.latitude, center.longitude)

        ne = d.destination(point, 45)
        sw = d.destination(point, 225)

        return Bounds(
            sw=Point(Decimal(sw.latitude), Decimal(sw.longitude)),
            ne=Point(Decimal(ne.latitude), Decimal(ne.longitude)))
Exemplo n.º 28
0
    def get_bounds(cls, center, distance):
        """
        Returns a Bounds object based on a center point and a distance.
        """

        d = geopy_distance(kilometers=distance)
        point = GeoPyPoint(center.latitude, center.longitude)

        ne = d.destination(point, 45)
        sw = d.destination(point, 225)

        return Bounds(
            sw=Point(Decimal(sw.latitude), Decimal(sw.longitude)), 
            ne=Point(Decimal(ne.latitude), Decimal(ne.longitude)))
Exemplo n.º 29
0
def compute_missing_distances(trackpoints):
    trackpoints = iter(trackpoints)
    p = next(trackpoints, None)
    if p is None:
        return
    if p.distance_meters is None:
        p.distance_meters = 0.0
    for t in trackpoints:
        if not t.distance_meters:
            t.distance_meters = p.distance_meters + geopy_distance(
                (p.latitude_degrees, p.longitude_degrees),
                (t.latitude_degrees, t.longitude_degrees),
            ).m
        p = t
Exemplo n.º 30
0
 def find_default_image(self, user_pnt):
     cities = SearchQuerySet().using('mobile_api').filter(django_ct='core.citypicture')\
                              .distance('geometry', user_pnt).order_by('distance')
     closest_city = cities[0]
     dist_btwn_city_user = geopy_distance((user_pnt.y, user_pnt.x), (closest_city.geometry.y, closest_city.geometry.x)).miles
     if dist_btwn_city_user <= closest_city.radius:
         default_image = closest_city.picture_url
     else:
         try:
             default = cities.filter(text='Default')[0]
             default_image = default.picture_url
         except:
             default_image = 'https://s3.amazonaws.com/pushpennyapp/pushpenny-default.jpg'
     return default_image
Exemplo n.º 31
0
def ponto_1(request):

    if request.method == 'POST':
        ulat1 = request.POST.get("latitude1")
        unome = request.POST.get("nome1")
        unome2 = request.POST.get("nome2")
        ulng1 = request.POST.get("longitude1")
        coord1 = fromstr('POINT(%s %s )' % (ulng1, ulat1), srid=4326)
        ulat2 = request.POST.get("latitude2")
        ulng2 = request.POST.get("longitude2")
        coord2 = fromstr('POINT(%s %s )' % (ulng2, ulat2), srid=4326)
        ud = geopy_distance(coord1, coord2)
        novo1 = ponto1(nome=unome,
                       ponto1=coord1,
                       ponto2=coord2,
                       nome2=unome2,
                       d=ud)
        novo1.save()

        d = geopy_distance(coord1, coord2)
        print d.meters
        return render_to_response('ponto_1.html')
    else:
        HttpResponse("ERRO")
Exemplo n.º 32
0
 def find_default_image(self, user_pnt):
     cities = SearchQuerySet().using('mobile_api').filter(django_ct='core.citypicture')\
                              .distance('geometry', user_pnt).order_by('distance')
     closest_city = cities[0]
     dist_btwn_city_user = geopy_distance(
         (user_pnt.y, user_pnt.x),
         (closest_city.geometry.y, closest_city.geometry.x)).miles
     if dist_btwn_city_user <= closest_city.radius:
         default_image = closest_city.picture_url
     else:
         try:
             default = cities.filter(text='Default')[0]
             default_image = default.picture_url
         except:
             default_image = 'https://s3.amazonaws.com/pushpennyapp/pushpenny-default.jpg'
     return default_image
Exemplo n.º 33
0
    def get_offers_within_miles(self, x=5):
        from offer.models import Offer
        from geopy.distance import distance as geopy_distance
        """
		# for testing
		print "All offers: %d"%Offer.objects.all().count()
		for i in Offer.objects.all():
			print "Customer location:", self.location.location
			print "Merchant location:", i.merchant.location.location
			print "Distance:", geopy_distance(self.location.location, i.merchant.location.location).mi
		"""

        return [
            i for i in Offer.objects.filter(expired_time__gt=datetime.now())
            if self.location and i.merchant.location and geopy_distance(
                self.location.location, i.merchant.location.location).mi <= x
        ]
Exemplo n.º 34
0
    def get_geo_distance(self, pre_ip, cur_ip):
        '''Caculate a distance in miles and meters with SRC IP and DST IP'''
        if self.is_ip(pre_ip) is False or self.is_ip(pre_ip) is False:
            return ""
        if self._g_geodb == None: return ""

        pre_gir = self._g_geodb.record_by_addr(pre_ip)
        pre_loc = []
        cur_loc = []
        if pre_gir != None:
            if pre_gir['latitude']: pre_loc.append(pre_gir['latitude'])
            if pre_gir['longitude']: pre_loc.append(pre_gir['longitude'])
        cur_gir = self._g_geodb.record_by_addr(cur_ip)

        if cur_gir != None:
            if cur_gir['latitude']: cur_loc.append(cur_gir['latitude'])
            if cur_gir['longitude']: cur_loc.append(cur_gir['longitude'])

        d = geopy_distance(pre_loc, cur_loc)
        return (d.meters, d.miles)
Exemplo n.º 35
0
def appendSortCSV(tempList):
    destList = []
    geolocator = Nominatim(user_agent="Hello")
    origin = geolocator.geocode("Stony Brook University")
    p1 = Point(origin.latitude, origin.longitude)
    for pos in range(len(tempList)):
        dest = geolocator.geocode(tempList[pos])
        p2 = Point(dest.latitude, dest.longitude)
        points = (p1, p2)
        destList.append({
            'Address':
            tempList[pos],
            'Distance':
            float(
                round(
                    sum(
                        geopy_distance(a, b).meters
                        for (a, b) in pairwise(points)) * 0.000621371, 4))
        })
    destList.sort(key=distance)
    for pos in range(len(destList)):
        print destList[pos]
Exemplo n.º 36
0
def findAllMatchingByDistance(sourceCollar, distance=None):
    """
    findAllMatchingByDistance This checks for interactions based on a distance parameter, dates must be precise
    """
    if not distance:
        return distance
    sourceCollarDataPoints = CollarData.objects.filter(collar=sourceCollar)
    matches = CollarData.objects.exclude(collar=sourceCollar)
    matchesToReport = []
    for sourceDataPoint in sourceCollarDataPoints:
        thesematches = matches.filter(GMT_DATETIME=sourceDataPoint.GMT_DATETIME)
        thesematches = thesematches.filter(LOCATION__distance_lte=(sourceDataPoint.LOCATION, D(km=distance)))
        if len(thesematches) > 0:
            for i,match in enumerate(thesematches):
                thisMatch = Collar()
                thisMatch.sourceCollarID = sourceDataPoint.collar.collarID
                thisMatch.matchedCollarID = match.collar.collarID
                thisMatch.datetime = sourceDataPoint.GMT_DATETIME
                thisMatch.locationX = sourceDataPoint.LOCATION.x
                thisMatch.locationY = sourceDataPoint.LOCATION.y
                thisMatch.distance = str(geopy_distance(sourceDataPoint.LOCATION,match.LOCATION))
                matchesToReport.append(thisMatch)
    return matchesToReport
Exemplo n.º 37
0
def findInteractionFromDataPoint(sourceCollarDataPoint, distance=settings.DEFAULT_INTERACTION_DISTANCE, timedelta=None, starttime=None, endtime=None, interactiongroup=None):
    """
    findInteractionFromDataPoint is used to calculate interaction data based on a singular data point.
    Once a match has been reported it is then added to the group.
    Input parameters should be self explanitory.
    """
    list_of_data_points_to_search = CollarData.objects.exclude(collar=sourceCollarDataPoint.collar)
    if starttime:
        if endtime:
            list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__range=(starttime, endtime))
        else:
            list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__gte=starttime)
    elif endtime:
        list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__lte=endtime)
    if distance:
        list_of_data_points_to_search = list_of_data_points_to_search.filter(LOCATION__distance_lte=(sourceCollarDataPoint.LOCATION, D(km=distance)))
    if timedelta:
        real_time_delta = datetime.timedelta(seconds=timedelta)
        start_delta = sourceCollarDataPoint.GMT_DATETIME - real_time_delta
        end_delta = sourceCollarDataPoint.GMT_DATETIME + real_time_delta
        list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME__range=(start_delta, end_delta))
    else:
        list_of_data_points_to_search = list_of_data_points_to_search.filter(GMT_DATETIME=sourceCollarDataPoint.GMT_DATETIME)
    for match in list_of_data_points_to_search:
        animal_interaction_to_add = AnimalInteraction()
        animal_interaction_to_add.source_animal_data_point = sourceCollarDataPoint
        animal_interaction_to_add.destination_animal_data_point = match
        animal_interaction_to_add.distance = geopy_distance(sourceCollarDataPoint.LOCATION,match.LOCATION).kilometers
        animal_interaction_to_add.interaction_group = interactiongroup
        try:
            animal_interaction_to_add.clean()
            animal_interaction_to_add.save()
        except ValidationError:
            print "The entry already exists"
    zip_content = ContentFile(exportShape())
    interactiongroup.zip_file.save("sampleFile.zip",zip_content)
    interactiongroup.save()
Exemplo n.º 38
0
    def _vincenty_distance(self,
                           origin: GeoLocation,
                           target: GeoLocation,
                           metric: bool = False) -> float:
        """Calculate distance between two locations using the proper Vincenty distance.

        .. note:: This is the proper way of discovering distance between lat/long
            coordinates. I would not recommend using the Haversine distance calculations
            in production as they have discrepancies of ~0.5% especially when using
            long distances.

        :param GeoLocation origin: The starting location
        :param GeoLocation target: The ending location
        :param bool metric: Return results in kilometers rather than miles,
            optional, defaults to False
        :return: The vincenty distance between the given coordinates
        :rtype: float
        """

        distance = geopy_distance((origin.latitude, origin.longitude),
                                  (target.latitude, target.longitude))
        if metric:
            return distance.km
        return distance.miles
Exemplo n.º 39
0
    def localinfo_return_response(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.create_localinfo_index_if_doesnt_exist()

        params_dict = request.GET
        params_keys = params_dict.keys()
        try:
            location_param = params_dict['location']
        except:
            response = {
                'error': {'message': "You must supply a valid user location information."}
            }
            return self.create_response(request, response)
        lat_lng_in_list = location_param.split(',')
        lat, lng = map(float, lat_lng_in_list)
        user_pnt = Point(lng, lat)
        radius = D(mi=float(params_dict['radius'])) if 'radius' in params_keys else D(mi=100)

        default_radius_filter = '5mi'
        filters = {
            "query": {
                "filtered" : {
                    "query" : {
                        "match_all" : {}
                    },
                    "filter" : {
                        "geo_distance" : {
                            "distance" : default_radius_filter,
                            "location" : location_param
                        }
                    }
                }
            },
            "facets": {
                "search_keyword" : {
                    "terms" : {
                        "field" : "search_keyword",
                        "order" : "count"
                    }
                },
                "search_category" : {
                    "terms" : {
                        "field" : "search_category",
                        "order" : "count"
                    }
                }
            }
        }

        res = self.es.search(index="localinfo", doc_type='populars', body=filters)
        popular_nearby_list_raw = res['facets']['search_keyword']['terms']
        # popular_category_list_raw = res['facets']['search_category']['terms'] # Not used for now; instead of parent categories.

        default_image = self.find_default_image(user_pnt)
        base_response = {
            "default_image": default_image,
            "search_categories": []
        }
        popular_category_sub_structure = {
            "name": "Popular Categories",
            "list": []
        }
        popular_nearby_sub_structure = {
            "name": "Popular Nearby",
            "list": []
        }

        # Always show parent categories as 'popular categories' (minus Special Interest and Product )
        for c in self.parent_categories_list:
            popular_category = self.return_popular_something_insert(user_pnt, c)
            popular_category_sub_structure['list'].append(popular_category)
        base_response['search_categories'].append(popular_category_sub_structure)

        # Always try to show 5 'popular nearby's based on historical searches (min occurance threshold at 100);
        # To the extent there aren't enough historical popular nearbys, then fill the rest with
        # randomly selected categories, provided that there are at least 10 deals available under each category.
        # The user's location & poular_nearbys pair is cached and used for 12hrs; If the user's location changes
        # more than 50 miles away from any cached locations, then generate a new list of popular nearbys.
        id_param = params_dict.get('id', 'uuid')
        cached_data = cache.get(id_param) if id_param != 'uuid' else None

        if cached_data:
            cached_locations = cached_data['pop_nearbys'].keys()
            closest_pnt_to_user = {}
            for location in cached_locations:
                lat, lng = map(float, location.split(','))
                dist_to_user = geopy_distance((user_pnt.y, user_pnt.x), (lat, lng)).miles
                if len(closest_pnt_to_user) == 0:
                    closest_pnt_to_user = {location: dist_to_user}
                    continue

                closest_dist_so_far = closest_pnt_to_user.values()[0]
                if dist_to_user < closest_dist_so_far:
                    closest_pnt_to_user = {location: dist_to_user}
                else:
                    continue

        max_dist_for_caching = 50 # miles
        if cached_data and (closest_pnt_to_user.values()[0] <= max_dist_for_caching):
            closest_lat_lng = closest_pnt_to_user.keys()[0]
            cached_popular_nearbys = cached_data['pop_nearbys'][closest_lat_lng]
            for n in cached_popular_nearbys:
                popular_nearby = self.return_popular_something_insert(user_pnt, n)
                popular_nearby_sub_structure['list'].append(popular_nearby)
            base_response['search_categories'].append(popular_nearby_sub_structure)
        else:
            max_nearbys = 5
            for kw in popular_nearby_list_raw[:max_nearbys]:
                if kw['count'] < 100:
                    break
                else:
                    popular_nearby = self.return_popular_something_insert(user_pnt, kw['term'])
                    popular_nearby_sub_structure['list'].append(popular_nearby)

            categories_to_sample_from = copy.copy(self.sanitized_categories_list)
            while True:
                popular_nearbys_so_far = [n['name'] for n in popular_nearby_sub_structure['list']]
                if len(popular_nearbys_so_far) >= max_nearbys:
                    break

                try:
                    random_pick = random.sample(categories_to_sample_from, 1)[0]
                except ValueError: # When ran out of categories to choose from.
                    break

                sqs = SearchQuerySet().using('mobile_api')\
                                      .filter(django_ct='core.coupon', online=False, is_duplicate=False,
                                              is_deleted=False, status='considered-active', mobilequery=random_pick)\
                                      .exclude(end__lt=datetime.now(pytz.utc))\
                                      .dwithin('merchant_location', user_pnt, radius)\
                                      .distance('merchant_location', user_pnt).order_by('distance')
                if (random_pick in popular_nearbys_so_far) or (len(sqs) < 10):
                    categories_to_sample_from.remove(random_pick)
                    continue
                else:
                    popular_nearby = self.return_popular_something_insert(user_pnt, random_pick)
                    popular_nearby_sub_structure['list'].append(popular_nearby)
                    categories_to_sample_from.remove(random_pick)
            popular_nearbys_so_far = [n['name'] for n in popular_nearby_sub_structure['list']]
            if popular_nearbys_so_far:
                base_response['search_categories'].append(popular_nearby_sub_structure)
                if id_param != 'uuid':
                    self.cache_it_with_location(id_param, popular_nearbys_so_far, location_param)

        # only for debugging purposes below
        # base_response['elasticsearch_res'] = res

        response = base_response
        return self.create_response(request, response)
Exemplo n.º 40
0
	def get_customers_within_miles(self,x=5):
		from geopy.distance import distance as geopy_distance
		return [ i.pk for i in Customer.objects.all() if i.location and self.location and geopy_distance(i.location.location,self.location.location).mi<=x]
Exemplo n.º 41
0
	def get_active_customers_miles(self, x=5, filter_pks=[]):
		from geopy.distance import distance as geopy_distance
		if len(filter_pks) > 0:
			return [ i.pk for i in Customer.objects.exclude(pk__in=filter_pks).filter(verified=True, active=True, offer_count=0) if i.location and self.location and geopy_distance(i.location.location,self.location.location).mi<=x]
		else:
			return [ i.pk for i in Customer.objects.filter(verified=True, active=True, offer_count=0) if i.location and self.location and geopy_distance(i.location.location,self.location.location).mi<=x]
Exemplo n.º 42
0
	def get_offers_within_miles(self,x=5):
		from offer.models import Offer
		from geopy.distance import distance as geopy_distance
		"""
		# for testing
		print "All offers: %d"%Offer.objects.all().count()
		for i in Offer.objects.all():
			print "Customer location:", self.location.location
			print "Merchant location:", i.merchant.location.location
			print "Distance:", geopy_distance(self.location.location, i.merchant.location.location).mi
		"""

		return [i for i in Offer.objects.filter(expired_time__gt=datetime.now()) if self.location and i.merchant.location and geopy_distance(self.location.location, i.merchant.location.location).mi<=x]
Exemplo n.º 43
0
	def get_merchants_within_miles(self,x=5):
		from geopy.distance import distance as geopy_distance
		#print self.address_1 + " " + self.zipcode.code, self.location.location.x, self.location.location.y
		#for i in Merchant.objects.all():
			#print i , i.location.location.x, i.location.location.y, geopy_distance(i.location.location,self.location.location).mi
		return [ i.pk for i in Merchant.objects.all() if i.location and self.location and geopy_distance(i.location.location,self.location.location).mi<=x]
Exemplo n.º 44
0
def find_laps(configuration: Configuration,
              segment,
              region=10,
              min_time=5,
              start_idx=0) -> List[Dict[str, Any]]:
    """Return laps given latitude & longitude data.

    We assume that the first point defines the start and
    that the last point defines the end and that these are
    approximately at the same place.

    Parameters
    ----------
    segment : dict
        The data for the full track.
    region : float
        The region around the starting point which is used to
        define if we are passing through the starting point and
        begin a new lap.
    min_time : float
        This is the minimum time (in seconds) we should spend in the
        region before exiting. This will depend on the setting for region
        and the velocity for the activity.
    start_idx : integer
        The starting point for the first lap.

    """
    if not segment:  # race not started yet
        return []

    points = [(pt['latitude'], pt['longitude']) for pt in segment]
    start = (configuration.start_latitude, configuration.start_longitude) \
        if configuration.start_latitude is not None and configuration.start_longitude \
        else points[start_idx]
    time = [pt['date'] for pt in segment]
    # For locating the tracks, we look for point which are such that
    # we enter the starting region and pass through it.
    # For each point, find the distance to the starting region:
    distance = [geopy_distance(point, start) for point in points]
    #np.set_printoptions(suppress=True)
    # print(distance)

    # Now look for points where we enter the region:
    # We want to avoid cases where we jump back and forth across the
    # boundary, so we set a minimum time we should spend inside the
    # region.

    # let's assume we are in pit or at the lap already at start time

    lapId = 1
    splits = []
    pit_entry_idx = None
    lap_entry_idx = None
    current_split = None

    for i, dist in enumerate(distance):
        if i == start_idx:  # in fact that just skips the first one
            if distance[0] > region:  # already at lap, so create new one
                current_split = LapSplit(lapId=lapId,
                                         pitEntryIdx=start_idx,
                                         pitLeaveIdx=start_idx,
                                         lapEntryIdx=start_idx)
            else:  # in pit
                current_split = LapSplit(
                    lapId=lapId,
                    pitEntryIdx=start_idx,
                )
            splits.append(current_split)  # HACK
            lapId += 1
            continue
        if dist <= region:  # we are inside the pit
            if distance[i - 1] <= region:  # was in pit before
                if pit_entry_idx:  # not recorded yet
                    delta_t = time[i] - time[pit_entry_idx]
                    if min_time < delta_t.total_seconds() or (
                            i == (len(distance) - 1)
                    ):  # check time in pit (if last point and in pit, create anyway
                        if current_split:  # long enough, dump the previous one to output list
                            current_split.lapLeaveIdx = pit_entry_idx
                            # HACK splits.append(current_split)
                        current_split = LapSplit(
                            lapId=str(lapId),
                            pitEntryIdx=pit_entry_idx)  # and create new one
                        splits.append(current_split)  # HACK
                        lapId += 1
                        pit_entry_idx = None
            else:  # entered the pit (left lap)
                pit_entry_idx = i  # remember entry, start measuring time
        else:  # outside of pit (on lap)
            if distance[i - 1] > region:  # was on lap before
                if lap_entry_idx:  # not recorded yet
                    delta_t = time[i] - time[lap_entry_idx]
                    if min_time < delta_t.total_seconds():  # check time in pit
                        if current_split:  # long enough, record switch to lap
                            current_split.pitLeaveIdx = lap_entry_idx
                            current_split.lapEntryIdx = lap_entry_idx
                        pit_entry_idx = None
            else:  # entered the lap (left pit)
                lap_entry_idx = i  # remember exit, start measuring time

    # HACK if current_split and current_split.lapEntryIdx:  # do not include the one having just pit time
    # HACK    splits.append(current_split)

    agg_splits = aggregate_splits(configuration, splits)

    # new
    statuses = extract_lap_statuses(configuration, agg_splits, segment)
    # TODO implement better way
    if not agg_splits[-1].lapLeaveIdx or distance[
            agg_splits[-1].lapLeaveIdx] > region:
        statuses[-1]['finished'] = False  # outside of region
    return statuses
Exemplo n.º 45
0
INCREMENT_MI = 5.0 # mile(s)
GROUPON_KEY = 'cd4f46230f4d6bb2c8467c1695e891f24a0baf37'
GROUPON_API_URL = 'http://api.groupon.com/v2/deals'

COLLECTED_DEALS = []

request_parameters = {
        'client_id': GROUPON_KEY,
        # 'radius': INCREMENT_MI
}

# denver bounding box: http://isithackday.com/geoplanet-explorer/index.php?woeid=24701640
north_east = Point(-103.705704,40.284950)
south_west = Point(-105.927017,39.128181)

x_dist_mi = geopy_distance((north_east.y, north_east.x), (north_east.y, south_west.x)).miles
y_dist_mi = geopy_distance((north_east.y, north_east.x), (south_west.y, north_east.x)).miles

x_grid_divisor = x_dist_mi / INCREMENT_MI
y_grid_divisor = y_dist_mi / INCREMENT_MI

x_increment_pnt = (north_east.x - south_west.x) / x_grid_divisor
y_increment_pnt = (north_east.y - south_west.y) / y_grid_divisor

x_move_count = int(math.ceil(x_grid_divisor)) + 1
y_move_count = int(math.ceil(y_grid_divisor)) + 1

# starting from south_west corner and move right across first, and move up
request_parameters['lat'] = south_west.y
request_parameters['lng'] = south_west.x
request_parameters['radius'] = 5
Exemplo n.º 46
0
    def deals_return_response(self, request, **kwargs):
        self.method_check(request, allowed=['get'])

        params_dict = request.GET
        params_keys = params_dict.keys()
        location_param = params_dict.get('location', None)
        if not location_param:
            response = {
                'error': {
                    'message':
                    "You must supply a valid user location information."
                }
            }
            return self.create_response(request, response)

        lat_lng_in_list = location_param.split(',')
        lat, lng = map(float, lat_lng_in_list)

        id_param = params_dict.get('id', 'uuid')

        radius = D(
            mi=float(params_dict['radius'])) if 'radius' in params_keys else D(
                mi=10)
        user_pnt = Point(lng, lat)
        sqs = SearchQuerySet().using('mobile_api').filter(django_ct='core.coupon', online=False,
                                                          is_duplicate=False, is_deleted=False, status='considered-active')\
                                                .exclude(end__lt=datetime.now(pytz.utc))\
                                                .dwithin('merchant_location', user_pnt, radius).distance('merchant_location', user_pnt)\
                                                .order_by('distance')

        if 'query' in params_keys:
            query = params_dict['query']
            sqs_by_query = SearchQuerySet().using('mobile_api').filter(
                mobilequery=query)
            sqs = sqs.__and__(sqs_by_query)

            # Prepare for 'localindex' api service
            self.create_localinfo_index_if_doesnt_exist()
            matched_category_indices = [
                i for i, s in enumerate(self.available_categories_list)
                if query.lower() in s.lower()
            ]
            matched_category_names = [
                self.available_categories_list[i]
                for i in matched_category_indices
            ]
            self.index_it_in_localinfo_populars(id_param, location_param,
                                                string.capwords(query),
                                                matched_category_names)

        if 'category_slugs' in params_keys:
            category_slugs_list = params_dict['category_slugs'].split(',')
            sqs_by_category = SearchQuerySet().using('mobile_api')
            for c in category_slugs_list:
                sqs_by_category = sqs_by_category.filter_or(
                    category_slugs=c.strip())
            sqs = sqs.__and__(sqs_by_category)

        if 'provider_slugs' in params_keys:
            provider_slugs_list = params_dict['provider_slugs'].split(',')
            sqs_by_provider = SearchQuerySet().using('mobile_api')
            for p in provider_slugs_list:
                sqs_by_provider = sqs_by_provider.filter_or(
                    provider_slugs=p.strip())
            sqs = sqs.__and__(sqs_by_provider)

        updated_after = params_dict.get('updated_after', None)
        per_page = int(params_dict.get('per_page', 20))
        page = int(params_dict.get('page', 1))
        start_point = (page - 1) * per_page
        end_point = page * per_page

        deals = []

        for sqs_obj in sqs[start_point:end_point]:
            merchant_pnt = sqs_obj.merchant_location
            if not merchant_pnt:
                continue
            dist_to_user = geopy_distance(
                (user_pnt.y, user_pnt.x),
                (merchant_pnt.y, merchant_pnt.x)).miles

            deal_description = sqs_obj.text
            if sqs_obj.related_deals_count != 0:
                deal_description = deal_description if deal_description else ""
                deal_description += "\n\nFind {} more similar deal(s) from this vendor on {}!".format(
                    sqs_obj.related_deals_count, sqs_obj.provider)
            each_deal = {
                'deal': {
                    'id':
                    sqs_obj.coupon_ref_id,
                    'title':
                    sqs_obj.embedly_title,
                    'short_title':
                    sqs_obj.embedly_description,
                    'description':
                    deal_description,
                    'fine_print':
                    sqs_obj.restrictions,
                    'number_sold':
                    None,
                    'url':
                    sqs_obj.link,
                    'untracked_url':
                    sqs_obj.directlink,
                    'price':
                    sqs_obj.price,
                    'value':
                    sqs_obj.listprice,
                    'discount_amount':
                    sqs_obj.discount,
                    'discount_percentage':
                    float(sqs_obj.percent) / 100,
                    'commission':
                    None,
                    'provider_name':
                    sqs_obj.provider,
                    'provider_slug':
                    sqs_obj.provider_slug,
                    'category_name':
                    ', '.join(sqs_obj.categories)
                    if sqs_obj.categories else None,
                    'category_slug':
                    ', '.join(sqs_obj.category_slugs)
                    if sqs_obj.category_slugs else None,
                    'image_url':
                    sqs_obj.image,
                    'online':
                    sqs_obj.online,
                    'expires_at':
                    sqs_obj.end,
                    'created_at':
                    sqs_obj.start,
                    'updated_at':
                    sqs_obj.lastupdated,
                    'is_duplicate':
                    sqs_obj.is_duplicate,
                    'merchant': {
                        'id': sqs_obj.merchant_ref_id,
                        'name': sqs_obj.merchant_name,
                        'address': sqs_obj.merchant_address,
                        'locality': sqs_obj.merchant_locality,
                        'region': sqs_obj.merchant_region,
                        'postal_code': sqs_obj.merchant_postal_code,
                        'country': "United States",
                        'country_code': "US",
                        'latitude': merchant_pnt.y,
                        'longitude': merchant_pnt.x,
                        'dist_to_user_mi': dist_to_user,
                        'url': sqs_obj.merchant_link,
                    }
                }
            }
            deals.append(each_deal)

        query = {
            'total':
            len(sqs),
            'page':
            page,
            'per_page':
            per_page,
            'query':
            query if 'query' in params_keys else None,
            'location': {
                'latitude': lat,
                'longitude': lng,
            },
            'radius':
            float(params_dict['radius']) if 'radius' in params_keys else 10,
            'online':
            False,
            'category_slugs':
            category_slugs_list if 'category_slugs' in params_keys else None,
            'provider_slugs':
            provider_slugs_list if 'provider_slugs' in params_keys else None,
            'updated_after':
            updated_after,
        }

        response = {
            'query': query,
            'deals': deals,
        }

        return self.create_response(request, response)
Exemplo n.º 47
0
    def localinfo_return_response(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.create_localinfo_index_if_doesnt_exist()

        params_dict = request.GET
        params_keys = params_dict.keys()
        try:
            location_param = params_dict['location']
        except:
            response = {
                'error': {
                    'message':
                    "You must supply a valid user location information."
                }
            }
            return self.create_response(request, response)
        lat_lng_in_list = location_param.split(',')
        lat, lng = map(float, lat_lng_in_list)
        user_pnt = Point(lng, lat)
        radius = D(
            mi=float(params_dict['radius'])) if 'radius' in params_keys else D(
                mi=100)

        default_radius_filter = '5mi'
        filters = {
            "query": {
                "filtered": {
                    "query": {
                        "match_all": {}
                    },
                    "filter": {
                        "geo_distance": {
                            "distance": default_radius_filter,
                            "location": location_param
                        }
                    }
                }
            },
            "facets": {
                "search_keyword": {
                    "terms": {
                        "field": "search_keyword",
                        "order": "count"
                    }
                },
                "search_category": {
                    "terms": {
                        "field": "search_category",
                        "order": "count"
                    }
                }
            }
        }

        res = self.es.search(index="localinfo",
                             doc_type='populars',
                             body=filters)
        popular_nearby_list_raw = res['facets']['search_keyword']['terms']
        # popular_category_list_raw = res['facets']['search_category']['terms'] # Not used for now; instead of parent categories.

        default_image = self.find_default_image(user_pnt)
        base_response = {
            "default_image": default_image,
            "search_categories": []
        }
        popular_category_sub_structure = {
            "name": "Popular Categories",
            "list": []
        }
        popular_nearby_sub_structure = {"name": "Popular Nearby", "list": []}

        # Always show parent categories as 'popular categories' (minus Special Interest and Product )
        for c in self.parent_categories_list:
            popular_category = self.return_popular_something_insert(
                user_pnt, c)
            popular_category_sub_structure['list'].append(popular_category)
        base_response['search_categories'].append(
            popular_category_sub_structure)

        # Always try to show 5 'popular nearby's based on historical searches (min occurance threshold at 100);
        # To the extent there aren't enough historical popular nearbys, then fill the rest with
        # randomly selected categories, provided that there are at least 10 deals available under each category.
        # The user's location & poular_nearbys pair is cached and used for 12hrs; If the user's location changes
        # more than 50 miles away from any cached locations, then generate a new list of popular nearbys.
        id_param = params_dict.get('id', 'uuid')
        cached_data = cache.get(id_param) if id_param != 'uuid' else None

        if cached_data:
            cached_locations = cached_data['pop_nearbys'].keys()
            closest_pnt_to_user = {}
            for location in cached_locations:
                lat, lng = map(float, location.split(','))
                dist_to_user = geopy_distance((user_pnt.y, user_pnt.x),
                                              (lat, lng)).miles
                if len(closest_pnt_to_user) == 0:
                    closest_pnt_to_user = {location: dist_to_user}
                    continue

                closest_dist_so_far = closest_pnt_to_user.values()[0]
                if dist_to_user < closest_dist_so_far:
                    closest_pnt_to_user = {location: dist_to_user}
                else:
                    continue

        max_dist_for_caching = 50  # miles
        if cached_data and (closest_pnt_to_user.values()[0] <=
                            max_dist_for_caching):
            closest_lat_lng = closest_pnt_to_user.keys()[0]
            cached_popular_nearbys = cached_data['pop_nearbys'][
                closest_lat_lng]
            for n in cached_popular_nearbys:
                popular_nearby = self.return_popular_something_insert(
                    user_pnt, n)
                popular_nearby_sub_structure['list'].append(popular_nearby)
            base_response['search_categories'].append(
                popular_nearby_sub_structure)
        else:
            max_nearbys = 5
            for kw in popular_nearby_list_raw[:max_nearbys]:
                if kw['count'] < 100:
                    break
                else:
                    popular_nearby = self.return_popular_something_insert(
                        user_pnt, kw['term'])
                    popular_nearby_sub_structure['list'].append(popular_nearby)

            categories_to_sample_from = copy.copy(
                self.sanitized_categories_list)
            while True:
                popular_nearbys_so_far = [
                    n['name'] for n in popular_nearby_sub_structure['list']
                ]
                if len(popular_nearbys_so_far) >= max_nearbys:
                    break

                try:
                    random_pick = random.sample(categories_to_sample_from,
                                                1)[0]
                except ValueError:  # When ran out of categories to choose from.
                    break

                sqs = SearchQuerySet().using('mobile_api')\
                                      .filter(django_ct='core.coupon', online=False, is_duplicate=False,
                                              is_deleted=False, status='considered-active', mobilequery=random_pick)\
                                      .exclude(end__lt=datetime.now(pytz.utc))\
                                      .dwithin('merchant_location', user_pnt, radius)\
                                      .distance('merchant_location', user_pnt).order_by('distance')
                if (random_pick in popular_nearbys_so_far) or (len(sqs) < 10):
                    categories_to_sample_from.remove(random_pick)
                    continue
                else:
                    popular_nearby = self.return_popular_something_insert(
                        user_pnt, random_pick)
                    popular_nearby_sub_structure['list'].append(popular_nearby)
                    categories_to_sample_from.remove(random_pick)
            popular_nearbys_so_far = [
                n['name'] for n in popular_nearby_sub_structure['list']
            ]
            if popular_nearbys_so_far:
                base_response['search_categories'].append(
                    popular_nearby_sub_structure)
                if id_param != 'uuid':
                    self.cache_it_with_location(id_param,
                                                popular_nearbys_so_far,
                                                location_param)

        # only for debugging purposes below
        # base_response['elasticsearch_res'] = res

        response = base_response
        return self.create_response(request, response)
Exemplo n.º 48
0
    def deals_return_response(self, request, **kwargs):
        self.method_check(request, allowed=['get'])

        params_dict = request.GET
        params_keys = params_dict.keys()
        location_param = params_dict.get('location', None)
        if not location_param:
            response = {
                'error': {'message': "You must supply a valid user location information."}
            }
            return self.create_response(request, response)

        lat_lng_in_list = location_param.split(',')
        lat, lng = map(float, lat_lng_in_list)

        id_param = params_dict.get('id', 'uuid')

        radius = D(mi=float(params_dict['radius'])) if 'radius' in params_keys else D(mi=10)
        user_pnt = Point(lng, lat)
        sqs = SearchQuerySet().using('mobile_api').filter(django_ct='core.coupon', online=False,
                                                          is_duplicate=False, is_deleted=False, status='considered-active')\
                                                .exclude(end__lt=datetime.now(pytz.utc))\
                                                .dwithin('merchant_location', user_pnt, radius).distance('merchant_location', user_pnt)\
                                                .order_by('distance')

        if 'query' in params_keys:
            query = params_dict['query']
            sqs_by_query = SearchQuerySet().using('mobile_api').filter(mobilequery=query)
            sqs = sqs.__and__(sqs_by_query)

            # Prepare for 'localindex' api service
            self.create_localinfo_index_if_doesnt_exist()
            matched_category_indices = [i for i, s in enumerate(self.available_categories_list) if query.lower() in s.lower()]
            matched_category_names = [self.available_categories_list[i] for i in matched_category_indices]
            self.index_it_in_localinfo_populars(id_param, location_param, string.capwords(query), matched_category_names)

        if 'category_slugs' in params_keys:
            category_slugs_list = params_dict['category_slugs'].split(',')
            sqs_by_category = SearchQuerySet().using('mobile_api')
            for c in category_slugs_list:
                sqs_by_category = sqs_by_category.filter_or(category_slugs=c.strip())
            sqs = sqs.__and__(sqs_by_category)

        if 'provider_slugs' in params_keys:
            provider_slugs_list = params_dict['provider_slugs'].split(',')
            sqs_by_provider = SearchQuerySet().using('mobile_api')
            for p in provider_slugs_list:
                sqs_by_provider = sqs_by_provider.filter_or(provider_slugs=p.strip())
            sqs = sqs.__and__(sqs_by_provider)

        updated_after = params_dict.get('updated_after', None)
        per_page = int(params_dict.get('per_page', 20))
        page = int(params_dict.get('page', 1))
        start_point = (page - 1) * per_page
        end_point = page * per_page

        deals = []

        for sqs_obj in sqs[start_point:end_point]:
            merchant_pnt = sqs_obj.merchant_location
            if not merchant_pnt:
                continue
            dist_to_user = geopy_distance((user_pnt.y, user_pnt.x), (merchant_pnt.y, merchant_pnt.x)).miles

            deal_description = sqs_obj.text
            if sqs_obj.related_deals_count != 0:
                deal_description = deal_description if deal_description else ""
                deal_description += "\n\nFind {} more similar deal(s) from this vendor on {}!".format(sqs_obj.related_deals_count,
                                                                                                      sqs_obj.provider)
            each_deal = {'deal':
                {
                    'id':                   sqs_obj.coupon_ref_id,
                    'title':                sqs_obj.embedly_title,
                    'short_title':          sqs_obj.embedly_description,
                    'description':          deal_description,
                    'fine_print':           sqs_obj.restrictions,
                    'number_sold':          None,
                    'url':                  sqs_obj.link,
                    'untracked_url':        sqs_obj.directlink,
                    'price':                sqs_obj.price,
                    'value':                sqs_obj.listprice,
                    'discount_amount':      sqs_obj.discount,
                    'discount_percentage':  float(sqs_obj.percent) / 100,
                    'commission':           None,
                    'provider_name':        sqs_obj.provider,
                    'provider_slug':        sqs_obj.provider_slug,
                    'category_name':        ', '.join(sqs_obj.categories) if sqs_obj.categories else None,
                    'category_slug':        ', '.join(sqs_obj.category_slugs) if sqs_obj.category_slugs else None,
                    'image_url':            sqs_obj.image,
                    'online':               sqs_obj.online,
                    'expires_at':           sqs_obj.end,
                    'created_at':           sqs_obj.start,
                    'updated_at':           sqs_obj.lastupdated,
                    'is_duplicate':         sqs_obj.is_duplicate,
                    'merchant': {
                        'id':               sqs_obj.merchant_ref_id,
                        'name':             sqs_obj.merchant_name,
                        'address':          sqs_obj.merchant_address,
                        'locality':         sqs_obj.merchant_locality,
                        'region':           sqs_obj.merchant_region,
                        'postal_code':      sqs_obj.merchant_postal_code,
                        'country':          "United States",
                        'country_code':     "US",
                        'latitude':         merchant_pnt.y,
                        'longitude':        merchant_pnt.x,
                        'dist_to_user_mi':  dist_to_user,
                        'url':              sqs_obj.merchant_link,
                    }
                }
            }
            deals.append(each_deal)

        query = {
            'total':                    len(sqs),
            'page':                     page,
            'per_page':                 per_page,
            'query':                    query if 'query' in params_keys else None,
            'location': {
                 'latitude':            lat,
                 'longitude':           lng,
            },
            'radius':                   float(params_dict['radius']) if 'radius' in params_keys else 10,
            'online':                   False,
            'category_slugs':           category_slugs_list if 'category_slugs' in params_keys else None,
            'provider_slugs':           provider_slugs_list if 'provider_slugs' in params_keys else None,
            'updated_after':            updated_after,
        }

        response = {
            'query': query,
            'deals': deals,
        }

        return self.create_response(request, response)
Exemplo n.º 49
0
def detalhe(request, codigo_inep):
	'''Essa rotina monta o territorio educativo com base no codigo inep da escola'''

	from django.contrib.gis.geos import GEOSGeometry
	from django.contrib.gis.geos import Point, Polygon, MultiPolygon
	from django.contrib.gis.measure import Distance, D
	from escola.models import Escola 
	from ibge.models import Municipio
	from ibge.models import SetorCensitario
	from censo_ibge.models import Domicilio01PorSetor
	from censo_ibge.models import Domicilio02PorSetor
	from censo_ibge.models import Responsavel01PorSetor
	from censo_ibge.models import Responsavel02PorSetor
	from censo_ibge.models import ResponsavelrendaPorSetor
	from censo_ibge.models import DomiciliorendaPorSetor
	from censo_ibge.models import PessoarendaPorSetor
	from censo_ibge.models import BasicoPorSetor
	from censo_ibge.models import Pessoa11PorSetor
	from censo_ibge.models import Pessoa12PorSetor
	from censo_ibge.models import Entorno01PorSetor
	from censo_ibge.models import Entorno02PorSetor
	from censo_ibge.models import Entorno03PorSetor
	from censo_ibge.models import Pessoa01PorSetor
	from censo_ibge.models import Pessoa02PorSetor
	from censo_ibge.models import Pessoa03PorSetor
	from escola.models import DadosEscolas
	from ideb.models import Ideb

	escola = Escola.objects.get(codigo_inep = codigo_inep)
	dados_escolas = DadosEscolas.objects.get(cod_inep = codigo_inep)

	ll_e = (escola.longitude, escola.latitude)

	latitude = str(escola.latitude).replace(',','.')
	longitude = str(escola.longitude).replace(',','.')

	# Pega o raio personalizado do municipio
	raio = Municipio.objects.get(cd_geocodm = escola.ibge.cd_geocodm).raio
	if raio == None:
		raio = 1

	# Calculos censo IBGE
	distancia_raio = Distance(km = raio)
	try:
		todos_setores = SetorCensitario.objects.filter(
			cd_geocodm = escola.ibge.cd_geocodm).filter(
			setor_censitario_bruto__distance_lte=(escola.latlong, distancia_raio))
	except:
		todos_setores = []
	
	domicilio_por_setor = {
	'casas': 0,
	'apartamentos': 0,
	'sem_agua': 0,
	'sem_banheiro': 0,
	'sem_lixo': 0,
	'sem_energia': 0,
	'casas_p': 0,
	'apartamentos_p': 0,
	'sem_agua_p': 0,
	'sem_banheiro_p': 0,
	'sem_lixo_p': 0,
	'sem_energia_p': 0,
	}

	moradores = {
	'casas': 0,
	'apartamentos': 0,
	'sem_agua': 0,
	'sem_banheiro': 0,
	'sem_lixo': 0,
	'sem_energia': 0,
	'casas_p': 0,
	'apartamentos_p': 0,
	'sem_agua_p': 0,
	'sem_banheiro_p': 0,
	'sem_lixo_p': 0,
	'sem_energia_p': 0,	
	}

	responsaveis = {
	'h_responsaveis': 0,
	'h_nao_alfabetizados': 0,
	'h_ate_18': 0,
	'h_sem_rendimento': 0,
	'h_rendimento_1sm': 0,
	'h_responsaveis_p': 0,
	'h_nao_alfabetizados_p': 0,
	'h_ate_18_p': 0,
	'h_sem_rendimento_p': 0,
	'h_rendimento_1sm_p': 0,
	'm_responsaveis': 0,
	'm_nao_alfabetizados': 0,
	'm_ate_18': 0,
	'm_sem_rendimento': 0,
	'm_rendimento_1sm': 0,
	'm_responsaveis_p': 0,
	'm_nao_alfabetizados_p': 0,
	'm_ate_18_p': 0,
	'm_sem_rendimento_p': 0,
	'm_rendimento_1sm_p': 0,
	't_responsaveis': 0,
	't_nao_alfabetizados': 0,
	't_ate_18': 0,
	't_sem_rendimento': 0,
	't_rendimento_1sm': 0,
	't_responsaveis_p': 0,
	't_nao_alfabetizados_p': 0,
	't_ate_18_p': 0,
	't_sem_rendimento_p': 0,
	't_rendimento_1sm_p': 0,
	}

	renda = {
	'dom_sem_rendimento': 0,
	'dom_ate_meio': 0,
	'dom_de_meio_a_1': 0,
	'dom_de_1_a_2': 0,
	'dom_de_2_a_3': 0,
	'dom_de_3_a_5': 0,
	'dom_de_5_a_10': 0,
	'dom_de_10_a_15': 0,
	'dom_de_15_a_20': 0,
	'dom_mais_de_20': 0,
	'dom_sem_rendimento_p': 0,
	'dom_ate_meio_p': 0,
	'dom_de_meio_a_1_p': 0,
	'dom_de_1_a_2_p': 0,
	'dom_de_2_a_3_p': 0,
	'dom_de_3_a_5_p': 0,
	'dom_de_5_a_10_p': 0,
	'dom_de_10_a_15_p': 0,
	'dom_de_15_a_20_p': 0,
	'dom_mais_de_20_p': 0,
	'h_sem_rendimento': 0,
	'h_ate_meio': 0,
	'h_de_meio_a_1': 0,
	'h_de_1_a_2': 0,
	'h_de_2_a_3': 0,
	'h_de_3_a_5': 0,
	'h_de_5_a_10': 0,
	'h_de_10_a_15': 0,
	'h_de_15_a_20': 0,
	'h_mais_de_20': 0,
	'm_sem_rendimento': 0,
	'm_ate_meio': 0,
	'm_de_meio_a_1': 0,
	'm_de_1_a_2': 0,
	'm_de_2_a_3': 0,
	'm_de_3_a_5': 0,
	'm_de_5_a_10': 0,
	'm_de_10_a_15': 0,
	'm_de_15_a_20': 0,
	'm_mais_de_20': 0,
	}

	piramide = {
	'h_100_mais': 0,
	'h_95_99': 0,
	'h_90_94': 0,
	'h_85_89': 0,
	'h_80_84': 0,
	'h_75_79': 0,
	'h_70_74': 0,
	'h_65_69': 0,
	'h_60_64': 0,
	'h_55_59': 0,
	'h_50_54': 0,
	'h_45_49': 0,
	'h_40_44': 0,
	'h_35_39': 0,
	'h_30_34': 0,
	'h_25_29': 0,
	'h_20_24': 0,
	'h_15_19': 0,
	'h_10_14': 0,
	'h_5_9': 0,
	'h_0_4': 0,
	'm_100_mais': 0,
	'm_95_99': 0,
	'm_90_94': 0,
	'm_85_89': 0,
	'm_80_84': 0,
	'm_75_79': 0,
	'm_70_74': 0,
	'm_65_69': 0,
	'm_60_64': 0,
	'm_55_59': 0,
	'm_50_54': 0,
	'm_45_49': 0,
	'm_40_44': 0,
	'm_35_39': 0,
	'm_30_34': 0,
	'm_25_29': 0,
	'm_20_24': 0,
	'm_15_19': 0,
	'm_10_14': 0,
	'm_5_9': 0,
	'm_0_4': 0,
	}

	infra = {
	'd_arborizacao_s': 0,
	'd_calcada_s': 0,
	'd_iluminacao_s': 0,
	'd_pavimentacao_s': 0,
	'd_rampa_cadeirante_s': 0,
	'd_esgoto_s': 0,
	'd_lixo_s': 0,
	'm_arborizacao_s': 0,
	'm_calcada_s': 0,
	'm_iluminacao_s': 0,
	'm_pavimentacao_s': 0,
	'm_rampa_cadeirante_s': 0,
	'm_esgoto_s': 0,
	'm_lixo_s': 0,
	'd_arborizacao_n': 0,
	'd_calcada_n': 0,
	'd_iluminacao_n': 0,
	'd_pavimentacao_n': 0,
	'd_rampa_cadeirante_n': 0,
	'd_esgoto_n': 0,
	'd_lixo_n': 0,
	'm_arborizacao_n': 0,
	'm_calcada_n': 0,
	'm_iluminacao_n': 0,
	'm_pavimentacao_n': 0,
	'm_rampa_cadeirante_n': 0,
	'm_esgoto_n': 0,
	'm_lixo_n': 0,

	}

	alfab = {
	'h_5_9_s': 0,
	'h_10_14_s': 0,
	'h_15_19_s': 0,
	'h_20_24_s': 0,
	'h_25_29_s': 0,
	'h_30_34_s': 0,
	'h_35_39_s': 0,
	'h_40_44_s': 0,
	'h_45_49_s': 0,
	'h_50_54_s': 0,
	'h_55_59_s': 0,
	'h_60_64_s': 0,
	'h_65_69_s': 0,
	'h_70_74_s': 0,
	'h_75_79_s': 0,
	'h_mais80_s': 0,
	'm_5_9_s': 0,
	'm_10_14_s': 0,
	'm_15_19_s': 0,
	'm_20_24_s': 0,
	'm_25_29_s': 0,
	'm_30_34_s': 0,
	'm_35_39_s': 0,
	'm_40_44_s': 0,
	'm_45_49_s': 0,
	'm_50_54_s': 0,
	'm_55_59_s': 0,
	'm_60_64_s': 0,
	'm_65_69_s': 0,
	'm_70_74_s': 0,
	'm_75_79_s': 0,
	'm_mais80_s': 0,
	'h_5_9_n': 0,
	'h_10_14_n': 0,
	'h_15_19_n': 0,
	'h_20_24_n': 0,
	'h_25_29_n': 0,
	'h_30_34_n': 0,
	'h_35_39_n': 0,
	'h_40_44_n': 0,
	'h_45_49_n': 0,
	'h_50_54_n': 0,
	'h_55_59_n': 0,
	'h_60_64_n': 0,
	'h_65_69_n': 0,
	'h_70_74_n': 0,
	'h_75_79_n': 0,
	'h_mais80_n': 0,
	'm_5_9_n': 0,
	'm_10_14_n': 0,
	'm_15_19_n': 0,
	'm_20_24_n': 0,
	'm_25_29_n': 0,
	'm_30_34_n': 0,
	'm_35_39_n': 0,
	'm_40_44_n': 0,
	'm_45_49_n': 0,
	'm_50_54_n': 0,
	'm_55_59_n': 0,
	'm_60_64_n': 0,
	'm_65_69_n': 0,
	'm_70_74_n': 0,
	'm_75_79_n': 0,
	'm_mais80_n': 0,
	}

	raca = {
	'branca': 0,
	'preta': 0,
	'amarela': 0,
	'parda': 0,
	'indigena': 0,
	}


	for s in todos_setores:
		try:
			racacor = Pessoa03PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			raca['branca'] += racacor.v002
			raca['preta'] += racacor.v003
			raca['amarela'] += racacor.v004
			raca['parda'] += racacor.v005
			raca['indigena'] += racacor.v006
		except:
			pass

		try:
			dom01 = Domicilio01PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			domicilio_por_setor['casas'] += dom01.s001
			domicilio_por_setor['apartamentos'] += dom01.v005
			domicilio_por_setor['sem_agua'] += dom01.s004
			domicilio_por_setor['sem_banheiro'] += dom01.v023
			domicilio_por_setor['sem_lixo'] += dom01.s005
			domicilio_por_setor['sem_energia'] += dom01.v046
			domicilio_por_setor['casas_p'] += dom01.ps001 / len(todos_setores)
			domicilio_por_setor['apartamentos_p'] += dom01.pv005 / len(todos_setores)
			domicilio_por_setor['sem_agua_p'] += dom01.ps004 / len(todos_setores)
			domicilio_por_setor['sem_banheiro_p'] += dom01.pv023 / len(todos_setores)
			domicilio_por_setor['sem_lixo_p'] += dom01.ps005 / len(todos_setores)
			domicilio_por_setor['sem_energia_p'] += dom01.pv046 / len(todos_setores)
		except:
			pass

		try:
			mor01 = Domicilio02PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			moradores['casas'] += mor01.s001
			moradores['apartamentos'] += mor01.v005
			moradores['sem_agua'] += mor01.s004
			moradores['sem_banheiro'] += mor01.v023
			moradores['sem_lixo'] += mor01.s005
			moradores['sem_energia'] += mor01.v041
			moradores['casas_p'] += mor01.ps001 / len(todos_setores)
			moradores['apartamentos_p'] += mor01.pv005 / len(todos_setores)
			moradores['sem_agua_p'] += mor01.ps004 / len(todos_setores)
			moradores['sem_banheiro_p'] += mor01.pv023 / len(todos_setores)
			moradores['sem_lixo_p'] += mor01.ps005 / len(todos_setores)
			moradores['sem_energia_p'] += mor01.pv041 / len(todos_setores)
		except:
			pass


		try:
			r01 = Responsavel01PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			r02 = Responsavel02PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			rrenda = ResponsavelrendaPorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))

	
			responsaveis['h_responsaveis'] += r02.v109
			responsaveis['h_responsaveis_p'] += r02.pv109 / len(todos_setores)
			responsaveis['h_nao_alfabetizados'] += r02.s004
			responsaveis['h_nao_alfabetizados_p'] += r02.ps004 / len(todos_setores)
			responsaveis['h_ate_18'] += r02.s003
			responsaveis['h_ate_18_p'] += r02.ps003 / len(todos_setores)
			responsaveis['h_sem_rendimento'] += rrenda.v032
			responsaveis['h_sem_rendimento_p'] += rrenda.pv032 / len(todos_setores)
			responsaveis['h_rendimento_1sm'] += rrenda.s002
			responsaveis['h_rendimento_1sm_p'] += rrenda.ps002 / len(todos_setores)
			responsaveis['m_responsaveis'] += r01.v001
			responsaveis['m_responsaveis_p'] += r01.pv001 / len(todos_setores)
			responsaveis['m_nao_alfabetizados'] += r01.s002
			responsaveis['m_nao_alfabetizados_p'] += r01.ps002 / len(todos_setores)
			responsaveis['m_ate_18'] += r01.s001
			responsaveis['m_ate_18_p'] += r01.ps001 / len(todos_setores)
			responsaveis['m_sem_rendimento'] += rrenda.v054
			responsaveis['m_sem_rendimento_p'] += rrenda.pv054 / len(todos_setores)
			responsaveis['m_rendimento_1sm'] += rrenda.s003
			responsaveis['m_rendimento_1sm_p'] += rrenda.ps003 / len(todos_setores)
			responsaveis['t_responsaveis'] += r02.v001
			#responsaveis['t_responsaveis_p'] += r02.pv001 / len(todos_setores)
			responsaveis['t_nao_alfabetizados'] += r02.s002
			#responsaveis['t_nao_alfabetizados_p'] += r02.ps002 / len(todos_setores)
			responsaveis['t_ate_18'] += r02.s001
			#responsaveis['t_ate_18_p'] += r02.ps001 / len(todos_setores)
			responsaveis['t_sem_rendimento'] += rrenda.v010
			responsaveis['t_sem_rendimento_p'] += rrenda.pv010 / len(todos_setores)
			responsaveis['t_rendimento_1sm'] += rrenda.s001
			responsaveis['t_rendimento_1sm_p'] += rrenda.ps001 / len(todos_setores)
		except:
			pass

		try:
			drenda = DomiciliorendaPorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
		except:
			drenda = False
			pass
		try:
			prenda = PessoarendaPorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
		except:
			prenda = False
			pass
		try:
			basico_setor = BasicoPorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
		except:
			basico_setor = False
			pass

		if drenda:
			renda['dom_sem_rendimento'] += drenda.v014
			renda['dom_ate_meio'] += drenda.s001
			renda['dom_de_meio_a_1'] += drenda.v008
			renda['dom_de_1_a_2'] += drenda.v009
			renda['dom_de_2_a_3'] += drenda.v010
			renda['dom_de_3_a_5'] += drenda.v011
			renda['dom_de_5_a_10'] += drenda.v012
			renda['dom_de_10_a_15'] += drenda.v013

		if prenda:
			renda['h_sem_rendimento'] += prenda.v032
			renda['h_ate_meio'] += prenda.v023
			renda['h_de_meio_a_1'] += prenda.v024
			renda['h_de_1_a_2'] += prenda.v025
			renda['h_de_2_a_3'] += prenda.v026
			renda['h_de_3_a_5'] += prenda.v027
			renda['h_de_5_a_10'] += prenda.v028
			renda['h_de_10_a_15'] += prenda.v029
			renda['h_de_15_a_20'] += prenda.v030
			renda['h_mais_de_20'] += prenda.v031

			renda['m_sem_rendimento'] += prenda.v054
			renda['m_ate_meio'] += prenda.v045
			renda['m_de_meio_a_1'] += prenda.v046
			renda['m_de_1_a_2'] += prenda.v047
			renda['m_de_2_a_3'] += prenda.v048
			renda['m_de_3_a_5'] += prenda.v049
			renda['m_de_5_a_10'] += prenda.v050
			renda['m_de_10_a_15'] += prenda.v051
			renda['m_de_15_a_20'] += prenda.v052
			renda['m_mais_de_20'] += prenda.v053

		try:
			psetor = Pessoa11PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			psetor2 = Pessoa12PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))

			piramide['h_100_mais'] += psetor.v134
			piramide['h_95_99'] += psetor.s020
			piramide['h_90_94'] += psetor.s019
			piramide['h_85_89'] += psetor.s018
			piramide['h_80_84'] += psetor.s017
			piramide['h_75_79'] += psetor.s016
			piramide['h_70_74'] += psetor.s015
			piramide['h_65_69'] += psetor.s014
			piramide['h_60_64'] += psetor.s013
			piramide['h_55_59'] += psetor.s012
			piramide['h_50_54'] += psetor.s011
			piramide['h_45_49'] += psetor.s010
			piramide['h_40_44'] += psetor.s009
			piramide['h_35_39'] += psetor.s008
			piramide['h_30_34'] += psetor.s007
			piramide['h_25_29'] += psetor.s006
			piramide['h_20_24'] += psetor.s005
			piramide['h_15_19'] += psetor.s004
			piramide['h_10_14'] += psetor.s003
			piramide['h_5_9'] += psetor.s002
			piramide['h_0_4'] += psetor.s001

			piramide['m_100_mais'] += psetor.v134
			piramide['m_95_99'] += psetor2.s020
			piramide['m_90_94'] += psetor2.s019
			piramide['m_85_89'] += psetor2.s018
			piramide['m_80_84'] += psetor2.s017
			piramide['m_75_79'] += psetor2.s016
			piramide['m_70_74'] += psetor2.s015
			piramide['m_65_69'] += psetor2.s014
			piramide['m_60_64'] += psetor2.s013
			piramide['m_55_59'] += psetor2.s012
			piramide['m_50_54'] += psetor2.s011
			piramide['m_45_49'] += psetor2.s010
			piramide['m_40_44'] += psetor2.s009
			piramide['m_35_39'] += psetor2.s008
			piramide['m_30_34'] += psetor2.s007
			piramide['m_25_29'] += psetor2.s006
			piramide['m_20_24'] += psetor2.s005
			piramide['m_15_19'] += psetor2.s004
			piramide['m_10_14'] += psetor2.s003
			piramide['m_5_9'] += psetor2.s002
			piramide['m_0_4'] += psetor2.s001
		except:
			pass

		try:
			entorno01 = Entorno01PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			entorno02 = Entorno03PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			infra['d_arborizacao_s'] += entorno01.s015
			infra['d_calcada_s'] += entorno01.s007
			infra['d_pavimentacao_s'] += entorno01.s005
			infra['d_rampa_cadeirante_s'] += entorno01.s013
			infra['d_esgoto_s'] += entorno01.s017
			infra['d_lixo_s'] += entorno01.s019 
			infra['d_iluminacao_s'] += entorno01.s003

			infra['d_arborizacao_n'] += entorno01.s016
			infra['d_calcada_n'] += entorno01.s008
			infra['d_pavimentacao_n'] += entorno01.s006
			infra['d_rampa_cadeirante_n'] += entorno01.s014
			infra['d_esgoto_n'] += entorno01.s018
			infra['d_lixo_n'] += entorno01.s020 
			infra['d_iluminacao_n'] += entorno01.s004

			infra['m_arborizacao_s'] += entorno02.s015
			infra['m_calcada_s'] += entorno02.s007
			infra['m_pavimentacao_s'] += entorno02.s005
			infra['m_rampa_cadeirante_s'] += entorno02.s013
			infra['m_esgoto_s'] += entorno02.s017
			infra['m_lixo_s'] += entorno02.s019 
			infra['m_iluminacao_s'] += entorno02.s003

			infra['m_arborizacao_n'] += entorno02.s016
			infra['m_calcada_n'] += entorno02.s008
			infra['m_pavimentacao_n'] += entorno02.s006
			infra['m_rampa_cadeirante_n'] += entorno02.s014
			infra['m_esgoto_n'] += entorno02.s018
			infra['m_lixo_n'] += entorno02.s020 
			infra['m_iluminacao_n'] += entorno02.s004


		except:
			pass

		try:
			p01 = Pessoa02PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))
			p02 = Pessoa01PorSetor.objects.using('censo_ibge').get(cod_setor = float(s.cd_geocodi))

			alfab['h_5_9_s'] += p01.s001
			alfab['h_10_14_s'] += p01.s002
			alfab['h_15_19_s'] += p01.s003
			alfab['h_20_24_s'] += p01.s004
			alfab['h_25_29_s'] += p01.s005
			alfab['h_30_34_s'] += p01.s006
			alfab['h_35_39_s'] += p01.s007
			alfab['h_40_44_s'] += p01.s008
			alfab['h_45_49_s'] += p01.s009
			alfab['h_50_54_s'] += p01.s010
			alfab['h_55_59_s'] += p01.s011
			alfab['h_60_64_s'] += p01.s012
			alfab['h_65_69_s'] += p01.s013
			alfab['h_70_74_s'] += p01.s014
			alfab['h_75_79_s'] += p01.s015
			alfab['h_mais80_s'] += p01.v077

			alfab['m_5_9_s'] += p01.s032
			alfab['m_10_14_s'] += p01.s033
			alfab['m_15_19_s'] += p01.s034
			alfab['m_20_24_s'] += p01.s035
			alfab['m_25_29_s'] += p01.s036
			alfab['m_30_34_s'] += p01.s037
			alfab['m_35_39_s'] += p01.s038
			alfab['m_40_44_s'] += p01.s039
			alfab['m_45_49_s'] += p01.s040
			alfab['m_50_54_s'] += p01.s041
			alfab['m_55_59_s'] += p01.s042
			alfab['m_60_64_s'] += p01.s043
			alfab['m_65_69_s'] += p01.s044
			alfab['m_70_74_s'] += p01.s045
			alfab['m_75_79_s'] += p01.s046
			alfab['m_mais80_s'] += p01.v162

			alfab['h_5_9_n'] += p01.s016
			alfab['h_10_14_n'] += p01.s017
			alfab['h_15_19_n'] += p01.s018
			alfab['h_20_24_n'] += p01.s019
			alfab['h_25_29_n'] += p01.s020
			alfab['h_30_34_n'] += p01.s021
			alfab['h_35_39_n'] += p01.s022
			alfab['h_40_44_n'] += p01.s023
			alfab['h_45_49_n'] += p01.s024
			alfab['h_50_54_n'] += p01.s025
			alfab['h_55_59_n'] += p01.s026
			alfab['h_60_64_n'] += p01.s027
			alfab['h_65_69_n'] += p01.s028
			alfab['h_70_74_n'] += p01.s029
			alfab['h_75_79_n'] += p01.s030
			alfab['h_mais80_n'] += p01.s031

			alfab['m_5_9_n'] += p01.s047
			alfab['m_10_14_n'] += p01.s048
			alfab['m_15_19_n'] += p01.s049
			alfab['m_20_24_n'] += p01.s050
			alfab['m_25_29_n'] += p01.s051
			alfab['m_30_34_n'] += p01.s052
			alfab['m_35_39_n'] += p01.s053
			alfab['m_40_44_n'] += p01.s054
			alfab['m_45_49_n'] += p01.s055
			alfab['m_50_54_n'] += p01.s056
			alfab['m_55_59_n'] += p01.s057
			alfab['m_60_64_n'] += p01.s058
			alfab['m_65_69_n'] += p01.s059
			alfab['m_70_74_n'] += p01.s060
			alfab['m_75_79_n'] += p01.s061
			alfab['m_mais80_n'] += p01.s062
		except:
			pass

	# Pega os equipamentos de saude
	from saude.models import Saude

	equipamentos_saude = []
	
	try: 
		for p in Saude.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			equipamentos_saude.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logra,
				'nome_logradouro': p.nome_logra,
				'numero_logradouro': p.num_lograd,
				'id': p.id,
				'distancia': distancia,
				})
	except:
		pass

	# Pega os teatros
	from teatro.models import Teatro

	teatros = []

	try:
		for p in Teatro.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			teatros.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})
	except:
		pass

	# pega os museus
	from museu.models import Museu

	museus = []

	try:
		for p in Museu.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			museus.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})

	except:
		pass

	# Pega as bibliotecas
	from biblioteca.models import Biblioteca

	bibliotecas = []

	try:
		for p in Biblioteca.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			bibliotecas.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})

	except:
		pass

	# Pega os centros de ciencia
	from centro_ciencia.models import CentroCiencia

	centros_ciencia = []

	try: 
		for p in CentroCiencia.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			centros_ciencia.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})

	except:
		pass

	# Pega CRAs
	from cra.models import Cra
	try:
		cras = Cra.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio))
	except:
		cras = []

	# Pega os cinemas
	from cinema.models import Cinema

	cinemas = []

	try:
		for p in Cinema.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters

			cinemas.append({
				'nome': p.nome,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})
	except:
		pass

	# Pega as salas verdes
	from sala_verde.models import SalaVerde

	salas_verdes = []

	try:
		for p in SalaVerde.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters
			salas_verdes.append({
				'nome': p.nome,
				'endereco': p.nome_logradouro,
				'id': p.id,
				'distancia': distancia				
				})
	except:
		pass

	# Pega os pontos de cultura
	from ponto_cultura.models import PontoCultura

	pontos_cultura = []

	try:
		for p in PontoCultura.objects.filter(geom__distance_lte = (escola.latlong, distancia_raio)).distance(escola.latlong).order_by('distance'):
			ll_p = (p.longitude, p.latitude)
			distancia = geopy_distance(ll_e, ll_p).meters

			pontos_cultura.append({
				'projeto': p.projeto,
				'tipo_logradouro': p.tipo_logradouro,
				'nome_logradouro': p.nome_logradouro,
				'numero_logradouro': p.numero_logradouro,
				'id': p.id,
				'distancia': distancia
				})
	except:
		pass


	# Soma domicilios
	try:
		total_domicilios = domicilio_por_setor['casas'] + domicilio_por_setor['apartamentos']
	except:
		total_domicilios = 0

	try:
		total_moradores = moradores['casas'] + moradores['apartamentos']
	except:
		total_moradores = 0

	try:
		numero_setores = len(todos_setores)
	except:
		numero_setores = 0

	try:
		media_moradores = total_moradores / total_domicilios
	except:
		media_moradores = 0

	ideb = Ideb.objects.get(codigo_inep = codigo_inep)

	return render_to_response('territorio/territorio.html', {
		'escola': escola,
		'latitude': latitude,
		'longitude': longitude,
		'setores': todos_setores,
		'domicilio_por_setor': domicilio_por_setor,
		'moradores': moradores,
		'responsaveis': responsaveis,
		'renda': renda,
		'piramide': piramide,
		'infra': infra,
		'alfab': alfab,
		'dados': dados_escolas,
		'equipamentos_saude': equipamentos_saude,
		'teatros': teatros,
		'museus': museus,
		'cinemas': cinemas,
		'salas_verdes': salas_verdes,
		'pontos_cultura': pontos_cultura,
		'bibliotecas': bibliotecas,
		'centros_ciencia': centros_ciencia,
		'cras': cras,
        'raca': raca,
		'total_domicilios': total_domicilios,
		'total_moradores': total_moradores,
		'numero_setores': numero_setores,
		'media_moradores': media_moradores,
		'ideb': ideb,
		})
Exemplo n.º 50
0
 def get_distance(self):
     return geopy_distance(
         (self.reference.position.y, self.reference.position.x),
         (self.colocated.position.y, self.colocated.position.x),
     )