class Geolocator(object):
    """
    Master geolocation class

    Uses Geocoder and GeoJSONer and Weightifier to find coordinates for and
    apply weights to all tagged locations.
    """
    def __init__(self):
        self.geocoder = Geocoder()
        self.geojsoner = GeoJSONer()
        self.weightifier = Weightifier()
        return

    def _build_container(self, locations):
        """
        Builds a LocationHitsContainer from the given locations

        :param list locations: list of app.models.Location objects to geolocate

        :returns: LocationHitsContainer
        """
        container = LocationHitsContainer()
        for l in locations:
            container.append(self.geocoder.geocode(l))
        return container

    def _apply_weights(self, container, weights, accuracy):
        """
        Uses the Weightifier to apply weights to the container

        :param LocationHitsContainer container: container of locations
        :param bool weights: flag indicating if weights should be calculated or
        not
        :param int accuracy: level of accuracy to use when calculating weights
        (must be greater than 0 and less than or equal to 5)

        :returns: modified container
        """
        if weights:
            if accuracy > 5:
                accuracy = 5
            container = self.weightifier.gather_all_names(container, accuracy)
            container = self.weightifier.weightify(container)
        return container

    def _build_geojson(self, container):
        """
        Iterates through locations in container and builds GeoJSON file

        :param LocationHitsContainer container: container of locations

        :returns: None
        """
        for hits in container.hits:
            for l in hits:
                self.geojsoner.append(l)
        return

    def geolocate(self, locations, weights=True, accuracy=1):
        """
        Given a list of tagged locations from the NLP tagger, this will convert
        each location to a app.geolocator.LocationWrap, find the coordinates of
        each, apply weighting, and convert to geojson

        :param list locations: list of app.models.Location objects to geolocate
        :param bool weights: flag indicating if weights should be calculated or
        not (defaults to True)
        :param int accuracy: level of accuracy to use when calculating weights
        (defaults to 1) (must be greater than 0 and less than or equal to 5)
            * 1 - weights will be found for all matches to countrycode
            * 2 - weights will be found for all matches to the above and
            admin1code
            * 3 - weights will be found for all matches to the above and
            admin2code
            * 4 - weights will be found for all matches to the above and
            admin3code
            * 5 - weights will be found for all matches to the above and
            admin4code

        :returns: None
        """
        # build the container
        container = self._build_container(locations)
        # apply weights
        container = self._apply_weights(container, weights, accuracy)
        # build the geojson
        self._build_geojson(container)
        return

    def geojson(self):
        """
        Returns the geojson of all geolocated locations

        :returns: geojson.FeatureCollection
        """
        return self.geojsoner.geojson()

    def __repr__(self):
        return "<Geolocator()>"
Пример #2
0
class Geolocator(object):
    """
    Master geolocation class

    Uses Geocoder and GeoJSONer and Weightifier to find coordinates for and
    apply weights to all tagged locations.
    """

    def __init__(self):
        self.geocoder = Geocoder()
        self.geojsoner = GeoJSONer()
        self.weightifier = Weightifier()
        return

    def _build_container(self, locations):
        """
        Builds a LocationHitsContainer from the given locations

        :param list locations: list of app.models.Location objects to geolocate

        :returns: LocationHitsContainer
        """
        container = LocationHitsContainer()
        for l in locations:
            container.append(self.geocoder.geocode(l))
        return container

    def _apply_weights(self, container, weights, accuracy):
        """
        Uses the Weightifier to apply weights to the container

        :param LocationHitsContainer container: container of locations
        :param bool weights: flag indicating if weights should be calculated or
        not
        :param int accuracy: level of accuracy to use when calculating weights
        (must be greater than 0 and less than or equal to 5)

        :returns: modified container
        """
        if weights:
            if accuracy > 5:
                accuracy = 5
            container = self.weightifier.gather_all_names(container, accuracy)
            container = self.weightifier.weightify(container)
        return container

    def _build_geojson(self, container):
        """
        Iterates through locations in container and builds GeoJSON file

        :param LocationHitsContainer container: container of locations

        :returns: None
        """
        for hits in container.hits:
            for l in hits:
                self.geojsoner.append(l)
        return

    def geolocate(self, locations, weights=True, accuracy=1):
        """
        Given a list of tagged locations from the NLP tagger, this will convert
        each location to a app.geolocator.LocationWrap, find the coordinates of
        each, apply weighting, and convert to geojson

        :param list locations: list of app.models.Location objects to geolocate
        :param bool weights: flag indicating if weights should be calculated or
        not (defaults to True)
        :param int accuracy: level of accuracy to use when calculating weights
        (defaults to 1) (must be greater than 0 and less than or equal to 5)
            * 1 - weights will be found for all matches to countrycode
            * 2 - weights will be found for all matches to the above and
            admin1code
            * 3 - weights will be found for all matches to the above and
            admin2code
            * 4 - weights will be found for all matches to the above and
            admin3code
            * 5 - weights will be found for all matches to the above and
            admin4code

        :returns: None
        """
        # build the container
        container = self._build_container(locations)
        # apply weights
        container = self._apply_weights(container, weights, accuracy)
        # build the geojson
        self._build_geojson(container)
        return

    def geojson(self):
        """
        Returns the geojson of all geolocated locations

        :returns: geojson.FeatureCollection
        """
        return self.geojsoner.geojson()

    def __repr__(self):
        return "<Geolocator()>"