Ticket #14107: address2.py

File address2.py, 5.1 KB (added by anonymous, 8 years ago)
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# address.py {Center}
5#
6# Copyright 2012 Hind <foxhind@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21# MA 02110-1301, USA.
22
23import sys
24import math
25import projections
26import urllib, urllib2, cookielib, Cookie
27import json
28from OsmData import OsmData, LON, LAT, TAG
29
30if sys.version_info[0] < 3:
31 reload(sys)
32 sys.setdefaultencoding("utf-8") # a hack to support UTF-8
33
34class client:
35 def __init__(self, proxy=None, user_agent='Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'):
36 self.redirect_handler = urllib2.HTTPRedirectHandler()
37 self.http_handler = urllib2.HTTPHandler()
38 self.opener = urllib2.build_opener(self.http_handler, self.redirect_handler)
39 if proxy:
40 self.proxy_handler = urllib2.ProxyHandler(proxy)
41 self.opener.add_handler(self.proxy_handler)
42 self.opener.addheaders = [('User-agent', user_agent)]
43 urllib2.install_opener(self.opener)
44 def request(self, url, params={}, timeout=5):
45 if params:
46 params = urllib.urlencode(params)
47 html = urllib2.urlopen(url, params, timeout)
48 else:
49 html = urllib2.urlopen(url)
50 return html.read()
51
52def main():
53 if len(sys.argv) != 2:
54 return 0
55
56 coords = (sys.argv[1].split(','))
57 # lon = float(coords[0])
58 # lat = float(coords[1])
59 # coords_m = projections.from4326((lon,lat), "EPSG:3857")
60
61 tData = OsmData()
62 httpc = client()
63
64 # text = httpc.request('http://pkk5.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreSelected/MapServer/1/query?text=&geometry='+str(coords_m[0])+','+str(coords_m[1])+'&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=*&f=pjson')
65
66 text = httpc.request('http://pkk5.rosreestr.ru/api/features/1?text='+coords[1]+'%20'+coords[0]+'&tolerance=4&limit=11')
67
68 data = json.loads(text)
69 if 'features' in data:
70 ids = []
71 for result in data['features']:
72 #if len(result['value']) >= 11:
73 try:
74 ids.append(result['attrs']['id']);
75 except KeyError:
76 continue
77
78 if len(ids) > 0:
79 addresses = []
80 for id in ids:
81 #text = httpc.request('http://maps.rosreestr.ru/arcgis/rest/services/Cadastre/CadastreInfo/MapServer/2/query?f=json&where=PARCELID%20IN%20(%27'+id+'%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=FULLADDRESS,CATEGORY,UTIL_BY_DOC');
82
83 text = httpc.request('http://pkk5.rosreestr.ru/api/features/1/'+id);
84
85 data = json.loads(text)
86
87 if 'feature' in data:
88 address = {}
89 try:
90 s = data['feature']['attrs']['address'].split(',')
91 address['addr:housenumber'] = s.pop().strip()
92 address['addr:street'] = s.pop().strip()
93 address['addr:full'] = data['feature']['attrs']['address']
94 address['fixme'] = 'yes'
95 except KeyError:
96 continue
97 try:
98 # address['category'] = feature['attributes']['CATEGORY']
99 address['utilization'] = data['feature']['attrs']['util_by_doc']
100 except KeyError:
101 pass
102 addresses.append(address)
103 else:
104 tData.addcomment('Feature is empty')
105 continue
106 count = len(addresses)
107 if count == 1:
108 nodeid = tData.addnode()
109 tData.nodes[nodeid][LON] = coords[0]
110 tData.nodes[nodeid][LAT] = coords[1]
111 tData.nodes[nodeid][TAG] = addresses[0]
112 comment = addresses[0]['addr:street'] + ', ' + addresses[0]['addr:housenumber']
113 if addresses[0]['utilization'] <> None:
114 comment += ' - ' + addresses[0]['utilization']
115 tData.addcomment(comment)
116 else:
117 for i in range(count):
118 angle = 2*math.pi*i/count
119 x = float(coords[0]) + 0.00001 * math.cos(angle)
120 y = float(coords[1]) + 0.00001 * math.sin(angle)
121 #node = projections.to4326((x, y), "EPSG:3857")
122 nodeid = tData.addnode()
123 tData.nodes[nodeid][LON] = x
124 tData.nodes[nodeid][LAT] = y
125 tData.nodes[nodeid][TAG] = addresses[i]
126 comment = addresses[i]['addr:street'] + ', ' + addresses[i]['addr:housenumber']
127 if addresses[i]['utilization'] <> None:
128 comment += ' - ' + addresses[i]['utilization']
129 tData.addcomment(comment)
130 else:
131 tData.addcomment('Unknown error')
132 else:
133 tData.addcomment('Features is empty')
134 tData.write(sys.stdout)
135 return 0
136
137if __name__ == '__main__':
138 main()