python - 自己的 Nominatim 服务器无法与 geopy 一起使用

标签 python geocoding geopy nominatim

我有一个包含超过 6k 个地址条目的数据库,因此我已经安装了带有 docker 的 nominatim 服务器来进行地理编码工作。它在“localhost:8080”上的 Web 表单中正常工作。

但是,当我尝试在 jupyter 中使用 geopy 进行查询时。它总是抛出错误。

我的 Jupyter 代码:

from geopy.geocoder import Nominatim
nom=Nominatim(domain='http://localhost:8080')
nom.geocode('some address') #the address works on the public server

错误堆栈:

---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1253             try:
-> 1254                 h.request(req.get_method(), req.selector, req.data, headers)
   1255             except OSError as err: # timeout error

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in request(self, method, url, body, headers)
   1105         """Send a complete request to the server."""
-> 1106         self._send_request(method, url, body, headers)
   1107 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers)
   1150             body = _encode(body, 'body')
-> 1151         self.endheaders(body)
   1152 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in endheaders(self, message_body)
   1101             raise CannotSendHeader()
-> 1102         self._send_output(message_body)
   1103 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in _send_output(self, message_body)
    933 
--> 934         self.send(msg)
    935         if message_body is not None:

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in send(self, data)
    876             if self.auto_open:
--> 877                 self.connect()
    878             else:

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in connect(self)
   1251 
-> 1252             super().connect()
   1253 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py in connect(self)
    848         self.sock = self._create_connection(
--> 849             (self.host,self.port), self.timeout, self.source_address)
    850         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    692     err = None
--> 693     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    694         af, socktype, proto, canonname, sa = res

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    731     addrlist = []
--> 732     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    733         af, socktype, proto, canonname, sa = res

gaierror: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
/Users/nmbqz/.virtualenvs/scrapy/lib/python3.5/site-packages/geopy/geocoders/base.py in _call_geocoder(self, url, timeout, raw, requester, deserializer, **kwargs)
    142         try:
--> 143             page = requester(req, timeout=(timeout or self.timeout), **kwargs)
    144         except Exception as error: # pylint: disable=W0703

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    162         opener = _opener
--> 163     return opener.open(url, data, timeout)
    164 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
    465 
--> 466         response = self._open(req, data)
    467 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in _open(self, req, data)
    483         result = self._call_chain(self.handle_open, protocol, protocol +
--> 484                                   '_open', req)
    485         if result:

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    443             func = getattr(handler, meth_name)
--> 444             result = func(*args)
    445             if result is not None:

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in https_open(self, req)
   1296             return self.do_open(http.client.HTTPSConnection, req,
-> 1297                 context=self._context, check_hostname=self._check_hostname)
   1298 

/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1255             except OSError as err: # timeout error
-> 1256                 raise URLError(err)
   1257             r = h.getresponse()

URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>

During handling of the above exception, another exception occurred:

GeocoderServiceError                      Traceback (most recent call last)
<ipython-input-141-ad9610064de4> in <module>()
----> 1 nom.geocode('Revontulentie 11')

/Users/nmbqz/.virtualenvs/scrapy/lib/python3.5/site-packages/geopy/geocoders/osm.py in geocode(self, query, exactly_one, timeout, addressdetails, language, geometry)
    191         logger.debug("%s.geocode: %s", self.__class__.__name__, url)
    192         return self._parse_json(
--> 193             self._call_geocoder(url, timeout=timeout), exactly_one
    194         )
    195 

/Users/nmbqz/.virtualenvs/scrapy/lib/python3.5/site-packages/geopy/geocoders/base.py in _call_geocoder(self, url, timeout, raw, requester, deserializer, **kwargs)
    169                 if "timed out" in message:
    170                     raise GeocoderTimedOut('Service timed out')
--> 171             raise GeocoderServiceError(message)
    172 
    173         if hasattr(page, 'getcode'):

GeocoderServiceError: [Errno 8] nodename nor servname provided, or not known

欣赏一些线索!

最佳答案

尝试从您的网址中删除 http:// 并将其添加为 scheme 参数。对于你的情况

nom=Nominatim(domain='localhost:8080', scheme='http')

这是由于源代码中的以下部分造成的

self.domain = domain.strip('/')
self.api = "%s://%s/search" % (self.scheme, self.domain)

其中 self.scheme 默认设置为 https

关于python - 自己的 Nominatim 服务器无法与 geopy 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362210/

相关文章:

python - 如何将无符号字节数组转换为ctypes中的base64字符串

python - 在 Python 中刷新文件内容是什么意思?

python - 获取字典中特定键的值

python - 使用 geopy 将英里转换为纬度和经度

python - 根据距离和方向计算点

c++ - 使用 ctypes 将结构传递给 native 库时遇到问题

javascript - Leaflet 和 geojson AJAX 调用

google-maps-api-3 - 使用 Googlemaps 地理编码器并存储数据

r - 使用for回路和georoute计算行驶距离时跳过错误

python - 计算距离很慢