Guest User

Untitled

a guest
Apr 7th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## geodecode
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2014 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8.  
  9. # Python modules
  10. import re
  11. import requests
  12. # NOC modules
  13. from noc.sa.models import *
  14. from noc.inv.models import *
  15. from noc.inv.models.object import *
  16.  
  17. rx_city = re.compile(
  18. r"^(г\.|пгт\.|пос\.|с\.)+(?P<city>(\S+ \S+ \S+|\S+ \S+|\S+))")
  19. rx_street = re.compile(
  20. r"^ ул\.+(?P<street>(\S+ \S+ \S+|\S+ \S+|\S+))")
  21. rx_house = re.compile(
  22. r"^ д\.+(?P<house>(\d+\S+|\d+))")
  23.  
  24. rx_room = re.compile(
  25. r"^ п\.+(?P<room>(\d+\S+|\d+))")
  26.  
  27. K = 0
  28. k = 0
  29.  
  30. for mo in tuple(ManagedObject.objects.filter(
  31. administrative_domain=1)):
  32. mo_id = mo.id
  33. name = mo.name
  34. postaddress = mo.postaddress.encode("UTF-8")
  35. #
  36. print ""
  37. # print postaddress
  38. #
  39. postaddress = postaddress.split(',')
  40. match = rx_city.search(postaddress[0])
  41. if match:
  42. city = match.group("city")
  43. #
  44. #
  45. else:
  46. continue
  47. try:
  48. match = rx_street.search(postaddress[1])
  49. except IndexError:
  50. continue
  51. if match:
  52. street = match.group("street")
  53. else:
  54. continue
  55. try:
  56. match = rx_house.search(postaddress[2])
  57. except IndexError:
  58. continue
  59. if match:
  60. house = match.group("house")
  61. else:
  62. continue
  63. try:
  64. match = rx_room.search(postaddress[3])
  65. except IndexError:
  66. continue
  67. if match:
  68. room = match.group("room")
  69. else:
  70. continue
  71.  
  72. pop = 'г.' + city + ', ул.' + street + ', д.' + house
  73. room = "Подъезд №" + room
  74.  
  75. K += 1
  76. if city:
  77.  
  78. print 'PoP = ', pop
  79. print 'room = ', room
  80.  
  81. try:
  82. url="http://geocode-maps.yandex.ru/1.x/?geocode=" + city + ",+" + street.replace(' ', '+') + ",+дом+" + house +"&format=json"
  83. urlopen = requests.get(url)
  84. obj = urlopen.json()
  85. coord = obj["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos"].split(' ')
  86. x = coord[0]
  87. y = coord[1]
  88.  
  89. print 'x = ', x
  90. print 'y = ', y
  91. print name,"\n"
  92.  
  93. k += 1
  94. except:
  95. continue
  96.  
  97. print K, k
Advertisement
Add Comment
Please, Sign In to add comment