25
25
import json
26
26
27
27
from .. import settings
28
+
28
29
USER_AGENT = settings .user_agent
29
30
31
+ DEFAULT_TIMEOUT = 2
32
+ REPROJ_TIMEOUT = 60
33
+
30
34
######################################
31
35
# EPSG.io
32
36
# https://github.com/klokantech/epsg.io
@@ -39,7 +43,7 @@ def ping():
39
43
url = "http://epsg.io"
40
44
try :
41
45
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
42
- urlopen (rq , timeout = 1 )
46
+ urlopen (rq , timeout = DEFAULT_TIMEOUT )
43
47
return True
44
48
except URLError as e :
45
49
log .error ('Cannot ping {} web service, {}' .format (url , e .reason ))
@@ -66,7 +70,7 @@ def reprojPt(epsg1, epsg2, x1, y1):
66
70
67
71
try :
68
72
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
69
- response = urlopen (rq ).read ().decode ('utf8' )
73
+ response = urlopen (rq , timeout = REPROJ_TIMEOUT ).read ().decode ('utf8' )
70
74
except (URLError , HTTPError ) as err :
71
75
log .error ('Http request fails url:{}, code:{}, error:{}' .format (url , err .code , err .reason ))
72
76
raise
@@ -110,7 +114,7 @@ def reprojPts(epsg1, epsg2, points):
110
114
111
115
try :
112
116
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
113
- response = urlopen (rq ).read ().decode ('utf8' )
117
+ response = urlopen (rq , timeout = REPROJ_TIMEOUT ).read ().decode ('utf8' )
114
118
except (URLError , HTTPError ) as err :
115
119
log .error ('Http request fails url:{}, code:{}, error:{}' .format (url , err .code , err .reason ))
116
120
raise
@@ -127,7 +131,7 @@ def search(query):
127
131
url = url .replace ("{QUERY}" , query )
128
132
log .debug ('Search crs : {}' .format (url ))
129
133
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
130
- response = urlopen (rq ).read ().decode ('utf8' )
134
+ response = urlopen (rq , timeout = DEFAULT_TIMEOUT ).read ().decode ('utf8' )
131
135
obj = json .loads (response )
132
136
log .debug ('Search results : {}' .format ([ (r ['code' ], r ['name' ]) for r in obj ['results' ] ]))
133
137
return obj ['results' ]
@@ -138,7 +142,7 @@ def getEsriWkt(epsg):
138
142
url = url .replace ("{CODE}" , str (epsg ))
139
143
log .debug (url )
140
144
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
141
- wkt = urlopen (rq ).read ().decode ('utf8' )
145
+ wkt = urlopen (rq , timeout = DEFAULT_TIMEOUT ).read ().decode ('utf8' )
142
146
return wkt
143
147
144
148
@@ -162,7 +166,7 @@ def reprojPt(epsg1, epsg2, x1, y1):
162
166
url = url .replace ("{CRS2}" , str (epsg2 ))
163
167
164
168
rq = Request (url , headers = {'User-Agent' : USER_AGENT })
165
- response = urlopen (rq ).read ().decode ('utf8' )
169
+ response = urlopen (rq , timeout = REPROJ_TIMEOUT ).read ().decode ('utf8' )
166
170
obj = json .loads (response )
167
171
168
172
return (float (obj ['point' ]['x' ]), float (obj ['point' ]['y' ]))
0 commit comments