Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. from noc.lib.nosql import get_db
  17.  
  18. rx_city = re.compile(
  19.         r"^(г\.|пгт\.|пос\.\.)+(?P<city>(\S+ \S+ \S+|\S+ \S+|\S+))")
  20. rx_street = re.compile(
  21.         r"^ ул\.+(?P<street>(\S+ \S+ \S+|\S+ \S+|\S+))")
  22. rx_house = re.compile(
  23.         r"^ д\.+(?P<house>(\d+\S+|\d+))")
  24.  
  25. rx_room = re.compile(
  26.         r"^ п\.+(?P<room>(\d+\S+|\d+))")
  27.  
  28. db = get_db()
  29. om = db.noc.objectmodels
  30.  
  31. map_mp = open('/tmp/map.mp.utf8', 'r')
  32. map_data = map_mp.read()
  33. map_mp.close()
  34.  
  35.  
  36. K = 0
  37. k = 0
  38.  
  39. for mo in tuple(ManagedObject.objects.filter(
  40.     administrative_domain=1)):
  41.     mo_id = mo.id
  42.     name = mo.name
  43.     postaddress = mo.postaddress.encode("UTF-8")
  44. #
  45. #    print ""
  46. #    print postaddress
  47. #
  48.     postaddress = postaddress.split(',')
  49.     match = rx_city.search(postaddress[0])
  50.     if match:
  51.         city = match.group("city")
  52.         for i in db.noc.objects.find({"name": 'г.' + city}):
  53.             city_id = i["_id"]
  54. #
  55. #
  56.     else:
  57.         continue
  58.     try:
  59.         match = rx_street.search(postaddress[1])
  60.     except IndexError:
  61.         continue
  62.     if match:
  63.         street = match.group("street")
  64.     else:
  65.         continue
  66.     try:
  67.         match = rx_house.search(postaddress[2])
  68.     except IndexError:
  69.         continue
  70.     if match:
  71.         house = match.group("house")
  72.     else:
  73.         continue
  74.     try:
  75.         match = rx_room.search(postaddress[3])
  76.     except IndexError:
  77.         continue
  78.     if match:
  79.         room = match.group("room")
  80.     else:
  81.         continue
  82.  
  83.  
  84.     pop =  "ул.".decode('UTF-8') + (street).decode('UTF-8') + ", д.".decode('UTF-8') + (house).decode('UTF-8')
  85.     room = "Подъезд №".decode('UTF-8') + room
  86.     rack = "Ящик №1".decode('UTF-8')
  87.  
  88.     K += 1
  89.     if city:
  90. #
  91.  
  92.         try:
  93.             url="http://geocode-maps.yandex.ru/1.x/?geocode=" + city + ",+" + street.replace(' ', '+') + ",+дом+" + house +"&format=json"
  94.             urlopen = requests.get(url)
  95.             obj = urlopen.json()
  96.             coord = obj["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos"].split(' ')
  97.             x = coord[0]
  98.             y = coord[1]
  99.             print pop, str(y), str(x)
  100.  
  101.             if not Object.objects.filter(name = pop):
  102.                 Object(
  103.                     name = pop,
  104.                     model = om.find_one({"name": "PoP | Access"})["_id"],
  105.                     container = ObjectId(city_id),
  106.                     data = {"geopoint": {
  107.                             "srid": "EPSG:4326",
  108.                             "x": float(x),
  109.                             "y": float(y),
  110.                             },
  111.                         "pop": {"level": 20}
  112.                         }
  113.                 ).save()
  114.                 for i in db.noc.objects.find({"container": ObjectId(city_id), "name": pop }):
  115.                     pop_id = i["_id"]
  116. #                print pop, pop_id
  117.  
  118.             if not Object.objects.filter(container = ObjectId(pop_id), name = room):
  119.                 Object(
  120.                     name = room,
  121.                     model = om.find_one({"name": "Room"})["_id"],
  122.                     container = ObjectId(pop_id),
  123.                     data = {}
  124.                 ).save()
  125.                 for i in db.noc.objects.find({"name": room}):
  126.                     room_id = i["_id"]
  127. #                print room, room_id
  128.  
  129.             if not Object.objects.filter(container = ObjectId(room_id), name = rack):
  130.                 Object(
  131.                     name = rack,
  132.                     model = om.find_one({"name": 'NoName | Rack | 19" 8U 800mm Shelf'})["_id"],
  133.                     container = ObjectId(room_id),
  134.                     data = {}
  135.                 ).save()
  136.                 for i in db.noc.objects.find({"name": room}):
  137.                     rack_id = i["_id"]
  138. #                print rack, rack_id
  139.  
  140.             # Уложить mo в Rack
  141.  
  142.             k += 1
  143.             print "\n\n"
  144.             break
  145.         except:
  146.             break
  147.             continue
  148.  
  149. print K, k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement