示例#1
0
 def _raise_for_error(self, body):
     err = body.get('status')
     if err:
         code = err['value']
         message = err['message']
         # http://www.geonames.org/export/webservice-exception.html
         if message.startswith("user account not enabled to use"):
             raise GeocoderInsufficientPrivileges(message)
         if code == 10:
             raise GeocoderAuthenticationFailure(message)
         if code in (18, 19, 20):
             raise GeocoderQuotaExceeded(message)
         raise GeocoderServiceError(message)
    def _parse_json(doc, exactly_one=True):  # pylint: disable=W0221
        """
        Parse a location name, latitude, and longitude from an JSON response.
        """
        status_code = doc.get("statusCode", 200)
        if status_code != 200:
            err = doc.get("errorDetails", "")
            if status_code == 401:
                raise GeocoderAuthenticationFailure(err)
            elif status_code == 403:
                raise GeocoderInsufficientPrivileges(err)
            elif status_code == 429:
                raise GeocoderQuotaExceeded(err)
            elif status_code == 503:
                raise GeocoderUnavailable(err)
            else:
                raise GeocoderServiceError(err)

        resources = doc['resourceSets'][0]['resources']
        if resources is None or not len(resources):  # pragma: no cover
            return None

        def parse_resource(resource):
            """
            Parse each return object.
            """
            stripchars = ", \n"
            addr = resource['address']

            address = addr.get('addressLine', '').strip(stripchars)
            city = addr.get('locality', '').strip(stripchars)
            state = addr.get('adminDistrict', '').strip(stripchars)
            zipcode = addr.get('postalCode', '').strip(stripchars)
            country = addr.get('countryRegion', '').strip(stripchars)

            city_state = join_filter(", ", [city, state])
            place = join_filter(" ", [city_state, zipcode])
            location = join_filter(", ", [address, place, country])

            latitude = resource['point']['coordinates'][0] or None
            longitude = resource['point']['coordinates'][1] or None
            if latitude and longitude:
                latitude = float(latitude)
                longitude = float(longitude)

            return Location(location, (latitude, longitude), resource)

        if exactly_one:
            return parse_resource(resources[0])
        else:
            return [parse_resource(resource) for resource in resources]
示例#3
0
文件: googlev3.py 项目: nickzam/geopy
 def _check_status(status):
     """
     Validates error statuses.
     """
     if status == 'OVER_QUERY_LIMIT':
         raise GeocoderQuotaExceeded(
             'The given key has gone over the requests limit in the 24'
             ' hour period or has submitted too many requests in too'
             ' short a period of time.')
     elif status == 'REQUEST_DENIED':
         raise GeocoderQueryError('Your request was denied.')
     elif status == 'INVALID_REQUEST':
         raise GeocoderQueryError('Probably missing address or latlng.')
     else:
         raise GeocoderQueryError('Unknown error.')
示例#4
0
 def _check_status(self, status):
     # https://developers.google.com/maps/documentation/geocoding/overview#StatusCodes
     if status == 'ZERO_RESULTS':
         # When there are no results, just return.
         return
     if status in ('OVER_QUERY_LIMIT', 'OVER_DAILY_LIMIT'):
         raise GeocoderQuotaExceeded(
             'The given key has gone over the requests limit in the 24'
             ' hour period or has submitted too many requests in too'
             ' short a period of time.')
     elif status == 'REQUEST_DENIED':
         raise GeocoderQueryError('Your request was denied.')
     elif status == 'INVALID_REQUEST':
         raise GeocoderQueryError('Probably missing address or latlng.')
     else:
         raise GeocoderQueryError('Unknown error.')
示例#5
0
 def _check_status(status):
     """
     Validates error statuses.
     """
     if status == 0:
         # When there are no results, just return.
         return
     if status == 110:
         raise GeocoderQueryError(u'请求来源未被授权.')
     elif status == 306:
         raise GeocoderQueryError(u'请求有护持信息请检查字符串.')
     elif status == 310:
         raise GeocoderAuthenticationFailure(u'请求参数信息有误.')
     elif status == 311:
         raise GeocoderQuotaExceeded(u'key格式错误.')
     else:
         raise GeocoderQueryError('Unknown error: %s' % status)
示例#6
0
    def _geocoder_exception_handler(self, error):
        """Custom exception handling for invalid queries and exceeded quotas.

        Geocod.io returns a ``422`` status code for invalid queries, which is not mapped
        in :const:`~geopy.geocoders.base.ERROR_CODE_MAP`. The service also returns a
        ``403`` status code for exceeded quotas instead of the ``429`` code mapped in
        :const:`~geopy.geocoders.base.ERROR_CODE_MAP`
        """
        if error.status_code == 422:
            error_message = self._get_error_message(error)
            raise GeocoderQueryError(error_message)
        if error.status_code == 403:
            error_message = self._get_error_message(error)
            quota_exceeded_snippet = "You can't make this request as it is " \
                                     "above your daily maximum."
            if quota_exceeded_snippet in error_message:
                raise GeocoderQuotaExceeded(error_message)
示例#7
0
    def _check_status(self, status):
        """
        Validates error statuses.
        """
        status_code = status['code']
        if status_code == 429:
            # Rate limit exceeded
            raise GeocoderQuotaExceeded(
                'The given key has gone over the requests limit in the 24'
                ' hour period or has submitted too many requests in too'
                ' short a period of time.')
        if status_code == 200:
            # When there are no results, just return.
            return

        if status_code == 403:
            raise GeocoderQueryError('Your request was denied.')
        else:
            raise GeocoderQueryError('Unknown error.')
示例#8
0
 def _geocoder_exception_handler(self, error, message):
     """
     LiveStreets-specific exceptions.
     """
     if "no active subscriptions found" in message.lower():
         raise GeocoderQuotaExceeded(message)
示例#9
0
 def _geocoder_exception_handler(self, error):  # pylint: disable=R0201
     """
     LiveStreets-specific exceptions.
     """
     if error.msg == "No active subscriptions found.":
         raise GeocoderQuotaExceeded(error.msg)
示例#10
0
 def _check_status(status):
     """
     Validates error statuses.
     """
     if status == '10000':
         # When there are no results, just return.
         return
     if status == '10001':
         raise GeocoderAuthenticationFailure('Invalid user key.')
     elif status == '10002':
         raise GeocoderAuthenticationFailure('Service not available.')
     elif status == '10003':
         raise GeocoderQuotaExceeded('Daily query over limit.')
     elif status == '10004':
         raise GeocoderQuotaExceeded('Access too frequent.')
     elif status == '10005':
         raise GeocoderQueryError('Invalid user IP.')
     elif status == '10006':
         raise GeocoderQueryError('Invalid user domain.')
     elif status == '10007':
         raise GeocoderQueryError('Invalid user signature')
     elif status == '10008':
         raise GeocoderQueryError('Invalid user scode.')
     elif status == '10009':
         raise GeocoderQuotaExceeded('Userkey plat nomatch.')
     elif status == '10010':
         raise GeocoderQuotaExceeded('IP query over limit.')
     elif status == '10011':
         raise GeocoderQuotaExceeded('Not support https.')
     elif status == '10012':
         raise GeocoderQuotaExceeded('Insufficient privileges.')
     elif status == '10013':
         raise GeocoderQuotaExceeded('User key recycled.')
     elif status == '10014':
         raise GeocoderQuotaExceeded('QPS has exceeded the limit.')
     elif status == '10015':
         raise GeocoderQuotaExceeded('Gateway timeout.')
     elif status == '10016':
         raise GeocoderQuotaExceeded('Server is busy.')
     elif status == '10017':
         raise GeocoderQuotaExceeded('Resource unavailable.')
     elif status == '20000':
         raise GeocoderQuotaExceeded('Invalid params.')
     elif status == '20001':
         raise GeocoderQuotaExceeded('Missing required params.')
     elif status == '20002':
         raise GeocoderQuotaExceeded('Illegal request.')
     elif status == '20003':
         raise GeocoderQuotaExceeded('Unknown error.')
     elif status == '20800':
         raise GeocoderQuotaExceeded('Out of service.')
     elif status == '20801':
         raise GeocoderQuotaExceeded('No roads nearby.')
     elif status == '20802':
         raise GeocoderQuotaExceeded('Route fail.')
     elif status == '20803':
         raise GeocoderQuotaExceeded('Over direction range.')
     elif status == '300**':
         raise GeocoderQuotaExceeded('Engine response data error.')
     else:
         raise GeocoderQueryError('Unknown error')
示例#11
0
    def handle(self, block_group, *args, **options):
        g = GoogleV3()
        cursor = connections['nfirs'].cursor()

        cursor.execute('SET transform_null_equals TO ON;')
        select = """
        select state, fdid, num_mile, street_pre, streetname, streettype, streetsuf, city, state_id, zip5, zip4, state, a.geom, count(*)
        from incidentaddress a
        where bkgpidfp10=%s and fdid<>(select fdid from firestation_firedepartment where id in (select department_for_block_group(%s)))
        and geocodable is not false
        group by state, fdid, num_mile, street_pre, streetname, streettype, streetsuf, city, state_id, zip5, zip4, state, a.geom
        """

        cmd = cursor.mogrify(select, (block_group, block_group))
        print cmd
        cursor.execute(cmd)
        over_quota_exceptions = 0

        for res in cursor.fetchall():

            if over_quota_exceptions >= 10:
                raise GeocoderQuotaExceeded('Too many consecutive timeouts.')

            print 'Row: ', res
            state, fdid, num_mile, street_pre, streetname, streettype, streetsuf, city, state_id, zip5, zip4, state_abbreviation, geom, count = res

            update = '''
            update incidentaddress
            set geom=ST_GeomFromText('POINT({results.longitude} {results.latitude})', 4326)
            {where}
            '''

            where_clause = cursor.mogrify(
                '''WHERE state=%s and fdid=%s and num_mile=%s and street_pre=%s and streetsuf=%s and city=%s
             and state_id=%s and zip4=%s and zip5=%s and streetname=%s''',
                (state, fdid, num_mile, street_pre, streetsuf, city, state_id,
                 zip4, zip5, streetname))

            if state_id == 'OO':
                state_id = ''

            if zip5 == '00000':
                zip5 = ''

            query_string = ' '.join([
                n for n in [
                    num_mile, street_pre, streetname, streettype, streetsuf,
                    city, state_id or state, zip5
                ] if n
            ])

            try:
                fd = FireDepartment.objects.filter(fdid=fdid,
                                                   state=state).first()
                bounds = None
                department_geom = None

                if fd:
                    department_geom = fd.geom or fd.headquarters_address.geom.buffer(
                        .5)

                    if department_geom:
                        xmin, ymin, xmax, ymax = department_geom.extent
                        bounds = [ymin, xmin, ymax, xmax]

                results = g.geocode(query=query_string, bounds=bounds)

                if not results:
                    continue

                results_point = Point(results.longitude, results.latitude)

                if geom:
                    geom = GEOSGeometry(geom)

                    if department_geom:
                        distance_improvement = department_geom.centroid.distance(
                            geom) - department_geom.centroid.distance(
                                results_point)

                        if distance_improvement <= -1:
                            self.stdout.write(
                                'Not updating since {} is a negative distance improvement'
                                .format(distance_improvement))
                            continue
                        else:
                            print 'Moving geometry from: {geom.x}, {geom.y} to {results.longitude}, {results.latitude}.'.format(
                                geom=geom, results=results)
                            print 'Distance improvement: {}'.format(
                                distance_improvement)

                if not options['dry_run']:
                    print update.format(results=results, where=where_clause)
                    cursor.execute(
                        update.format(results=results, where=where_clause))

                self.stdout.write('{} rows updated'.format(cursor.rowcount))

            except GeocoderQuotaExceeded:
                self.stdout.write('Geocoding Quota exceeded.')
                over_quota_exceptions += 1
                sleep(1)
                continue
            except GeocoderTimedOut:
                self.stdout.write('Geocoder Timed Out.')
                sleep(1)
                continue
            except GeocoderServiceError:
                self.stdout.write('Geocoder Timed Out.')
                sleep(1)
                continue
            over_quota_exceptions = 0
            print '\n'

        cursor.execute('SET transform_null_equals TO OFF;')