首页 > 解决方案 > 反向地理编码 - 无法建立连接,因为目标机器主动拒绝它'

问题描述

我有一个免费增值帐户并生成了 2 个 REST API 密钥。使用任一键时出现以下错误 -

HTTPConnectionPool(host='batch.geocoder.ls.hereapi.com', port=80): 最大重试次数超出 url: /6.2/jobs? prox=%5B24.47%2C+24.78%2C+48.2%2C+-33.87%2C+50.85%2C+26.21%2C+-22.92%2C+-22.34%2C+50.91%2C+47.41%2C+-33.39%2C+39.92 %2C+39.79%5D%2C%5B54.34%2C+55.6%2C+16.36%2C+151.21%2C+4.36%2C+50.57%2C+-43.24%2C+-49.05%2C+-114.07%2C+8.54%2C+ -70.52%2C+116.17%2C+116.51%5D&mode=retrieveAddresses&maxresults=1&gen=9&apiKey=-l7nmoOoBBAH3fHWUg2QYL6FB3qLcEvb88MRFTUuE5A (由NewConnectionError(': 无法建立新连接: [WinError 10061] 目标机器主动拒绝) 无法建立连接它'))

我正在关注 [ https://developer.here.com/blog/reverse-geocoding-a-location-using-python] 我还按照文档来构建我的 GET 请求 [ https://developer.here.com/documentation /examples/rest/geocoder/reverse-geocode]我不知道发生了什么。过去一周我一直试图弄清楚这一点。它超出了我的范围。我是编程新手,python,stackoverflow ....一切!技术支持,你能指点一下吗?谢谢。

我正在使用 Python。目标是对 CSV 文件中的 25,000 个点进行反向地理编码并写入 CSV 文件。我的反向地理编码代码 -

# Request URL
request_url = "https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json"

# API key
#api_key1 = 'MY API KEY 1'
api_key2 = 'MY API KEY 2'

# setting the parameters to be sent along with the request_url in the GET request
parametres = {
                'prox': '{},{}'.format(latitude_list,longitude_list),
                'mode': 'retrieveAddresses',
                'maxresults': 1,
                'gen': 9,
                'apiKey': api_key2                 
             }

# send a GET request
response = requests.get (url = request_url, params = parametres)
print(response.status_code)

# check if the request was successful and get the JSON response data
if response.status_code == 200:
    print('Request successful.')
    data = response.json()
    print(data)
else:
    print('Request failed.')

标签: here-api

解决方案


正确的工作代码-

import random
import time
import pandas as pd
import requests


latitude = 12.9716
longitude = 77.5946
file_name = 'data.csv'


def generate_random_data(lat, lon, num_rows, file_name):
    with open(file_name, 'a') as output:
        output.write('timestamp,latitude,longitude\n')
        for _ in range(num_rows):
            time_stamp = time.time()
            generated_lat = random.random()/100
            generated_lon = random.random()/100
            output.write("{},{},{}\n".format(
                time_stamp, lat+generated_lat, lon+generated_lon))


generate_random_data(latitude, longitude, 10, file_name)

# To read 6th row data
data = pd.read_csv("data.csv", skiprows=5, nrows=1)

# To print the values
print(data)

# To convert values into list
list_value = data.values.tolist()

# Latitude and Longitude
latitude = list_value[0][1]
longitude = list_value[0][2]

print(latitude, longitude)

request_url = "https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json"

# API key

api_key = 'your rest api key'

# setting the parameters to be sent along with the request_url in the GET request
parametres = {
    'prox': '{},{}'.format(latitude, longitude),
    'mode': 'retrieveAddresses',
    'maxresults': 1,
    'gen': 9,
    'apiKey': api_key
}

# send a GET request
response = requests.get(url=request_url, params=parametres)
print(response.status_code)

# check if the request was successful and get the JSON response data
if response.status_code == 200:
    print('Request successful.')
    data = response.json()
    print(data)
else:
    print('Request failed.')

它给出了以下响应-

请求成功。{'Response': {'MetaInfo': {'Timestamp': '2020-06-17T11:50:24.706+0000'}, 'View': [{'_type': 'SearchResultsViewType', 'ViewId': 0, 'Result': [{'Relevance': 1.0, 'Distance': -429.0, 'Direction': 16.2, 'MatchLevel': 'district', 'MatchQuality': {'Country': 1.0, 'State': 1.0, '县':1.0,'城市':1.0,'区':1.0,'邮政编码':1.0},'位置':{'LocationId':'NT_ncA49aCPF2GKSWJE4kP26C','LocationType':'区域','DisplayPosition': {'纬度':12.98721,'经度':77.60402},'MapView':{'TopLeft':{'纬度':12.99579,'经度':77.58896},'BottomRight':


推荐阅读