Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- ##----------------------------------------------------------------------
- ## geodecode
- ##----------------------------------------------------------------------
- ## Copyright (C) 2007-2014 The NOC Project
- ## See LICENSE for details
- ##----------------------------------------------------------------------
- # Python modules
- import re
- import requests
- # NOC modules
- from noc.sa.models import *
- from noc.inv.models import *
- from noc.inv.models.object import *
- from noc.lib.nosql import get_db
- rx_city = re.compile(
- r"^(г\.|пгт\.|пос\.|с\.)+(?P<city>(\S+ \S+ \S+|\S+ \S+|\S+))")
- rx_street = re.compile(
- r"^ ул\.+(?P<street>(\S+ \S+ \S+|\S+ \S+|\S+))")
- rx_house = re.compile(
- r"^ д\.+(?P<house>(\d+\S+|\d+))")
- rx_room = re.compile(
- r"^ п\.+(?P<room>(\d+\S+|\d+))")
- db = get_db()
- om = db.noc.objectmodels
- map_mp = open('/tmp/map.mp.utf8', 'r')
- map_data = map_mp.read()
- map_mp.close()
- K = 0
- k = 0
- for mo in tuple(ManagedObject.objects.filter(
- administrative_domain=1)):
- mo_id = mo.id
- name = mo.name
- postaddress = mo.postaddress.encode("UTF-8")
- #
- # print ""
- # print postaddress
- #
- postaddress = postaddress.split(',')
- match = rx_city.search(postaddress[0])
- if match:
- city = match.group("city")
- for i in db.noc.objects.find({"name": 'г.' + city}):
- city_id = i["_id"]
- #
- #
- else:
- continue
- try:
- match = rx_street.search(postaddress[1])
- except IndexError:
- continue
- if match:
- street = match.group("street")
- else:
- continue
- try:
- match = rx_house.search(postaddress[2])
- except IndexError:
- continue
- if match:
- house = match.group("house")
- else:
- continue
- try:
- match = rx_room.search(postaddress[3])
- except IndexError:
- continue
- if match:
- room = match.group("room")
- else:
- continue
- pop = "ул.".decode('UTF-8') + (street).decode('UTF-8') + ", д.".decode('UTF-8') + (house).decode('UTF-8')
- room = "Подъезд №".decode('UTF-8') + room
- rack = "Ящик №1".decode('UTF-8')
- K += 1
- if city:
- #
- try:
- url="http://geocode-maps.yandex.ru/1.x/?geocode=" + city + ",+" + street.replace(' ', '+') + ",+дом+" + house +"&format=json"
- urlopen = requests.get(url)
- obj = urlopen.json()
- coord = obj["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos"].split(' ')
- x = coord[0]
- y = coord[1]
- print pop, str(y), str(x)
- if not Object.objects.filter(name = pop):
- Object(
- name = pop,
- model = om.find_one({"name": "PoP | Access"})["_id"],
- container = ObjectId(city_id),
- data = {"geopoint": {
- "srid": "EPSG:4326",
- "x": float(x),
- "y": float(y),
- },
- "pop": {"level": 20}
- }
- ).save()
- for i in db.noc.objects.find({"container": ObjectId(city_id), "name": pop }):
- pop_id = i["_id"]
- # print pop, pop_id
- if not Object.objects.filter(container = ObjectId(pop_id), name = room):
- Object(
- name = room,
- model = om.find_one({"name": "Room"})["_id"],
- container = ObjectId(pop_id),
- data = {}
- ).save()
- for i in db.noc.objects.find({"name": room}):
- room_id = i["_id"]
- # print room, room_id
- if not Object.objects.filter(container = ObjectId(room_id), name = rack):
- Object(
- name = rack,
- model = om.find_one({"name": 'NoName | Rack | 19" 8U 800mm Shelf'})["_id"],
- container = ObjectId(room_id),
- data = {}
- ).save()
- for i in db.noc.objects.find({"name": room}):
- rack_id = i["_id"]
- # print rack, rack_id
- # Уложить mo в Rack
- k += 1
- print "\n\n"
- break
- except:
- break
- continue
- print K, k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement