Exemple #1
0
    def location(self, id):
        """
        Return geo address for Managed Objects
        """
        def chunkIt(seq, num):
            avg = len(seq) / float(num)
            out = []
            last = 0.0

            while last < len(seq):
                out.append(seq[int(last):int(last + avg)])
                last += avg
            return out

        location = []
        address = Object.get_by_id(id).get_address_text()
        if address:
            for res in address.split(","):
                adr = normalize_division(res.strip().decode("utf-8").lower())
                if None in adr and "" in adr:
                    continue
                if None in adr:
                    location += [adr[1].title().strip()]
                else:
                    location += [' '.join(adr).title().strip()]
            res = chunkIt(location, 2)
            location_1 = ", ".join(res[0])
            location_2 = ", ".join(res[1])
            return [location_1, location_2]
        else:
            return ["", ""]
Exemple #2
0
 def create_division(self, oktmo):
     """
     Create division by OKATO object
     :returns: Division
     """
     if oktmo in self.div_cache:
         return self.div_cache[oktmo]
     o = self.oktmo[oktmo]
     d = Division.objects.filter(data__OKTMO=oktmo).first()
     if not d:
         if o.parent:
             p = self.oktmo[o.parent].oktmo
             parent = self.create_division(p)
         else:
             parent = self.get_top()
         ao = self.get_addrobj(oktmo=oktmo)
         self.info("Creating %s" % o.name)
         data = {
             "OKTMO": oktmo
         }
         if ao:
             name = ao["offname"]
             short_name = ao["shortname"]
             if ao.get("okato", "").strip():
                 data["OKATO"] = ao["okato"]
             if ao.get("kladr", "").strip():
                 data["KLADR"] = ao["kladr"]
             data["FIAS_AOID"] = ao["aoid"]
             data["FIAS_AOGUID"] = ao["aoguid"]
             data["FIAS_CODE"] = ao["code"]
         else:
             name = o.name
             short_name = None
         if not short_name:
             short_name, name = normalize_division(name)
         d = Division(
             name=name,
             short_name=short_name,
             parent=parent,
             data=data
         )
         d.save()
     self.div_cache[oktmo] = d
     return d
Exemple #3
0
 def create_division2(self, ao):
     """
     Create division by ADDROBJ, used when OKATO is empty
     :returns: Division
     """
     if ao["aoid"] in self.aoid_cache:
         return self.aoid_cache[ao["aoid"]]
     d = Division.objects.filter(data__FIAS_AOID=ao["aoid"]).first()
     if not d:
         if ao["parentguid"]:
             po = self.get_addrobj(aoguid=ao["parentguid"])
             if po["oktmo"]:
                 parent = self.create_division(po["oktmo"])
             else:
                 parent = self.create_division2(po)
         else:
             parent = self.get_top()
         self.info("Creating %s" % ao["offname"])
         data = {}
         name = ao["offname"]
         short_name = ao["shortname"]
         if ao.get("okato", "").strip():
             data["OKATO"] = ao["okato"]
         if ao.get("oktmo", "").strip():
             data["OKTMO"] = ao["oktmo"]
         if ao.get("kladr", "").strip():
             data["KLADR"] = ao["kladr"]
         data["FIAS_AOID"] = ao["aoid"]
         data["FIAS_AOGUID"] = ao["aoguid"]
         data["FIAS_CODE"] = ao["code"]
         if not short_name:
             short_name, name = normalize_division(name)
         d = Division(
             name=name,
             short_name=short_name,
             parent=parent,
             data=data
         )
         d.save()
     self.aoid_cache[ao["aoid"]] = d
     return d