def get_layer_objects(self, layer, x0, y0, x1, y1, srid): """ Extract GeoJSON from bounding box """ lr = Layer.get_by_code(layer) if not lr: return {} bbox = self.get_bbox(x0, y0, x1, y1, srid) features = [ geojson.Feature( id=str(d["_id"]), geometry=self.transform(d["point"], self.db_proj, srid), properties={ "object": str(d["_id"]), "label": d.get("name", "") }, ) for d in Object._get_collection().find( { "layer": lr.id, "point": { "$geoWithin": { "$geometry": bbox } } }, { "_id": 1, "point": 1, "label": 1 }, ) ] return geojson.FeatureCollection(features=features, crs=srid)
def update_links(self): if not self.pop: return level = self.pop.get_data("pop", "level") linked = self.get_linked_pops() for c, pop, layer in self.iter_db_links(level): if pop in linked: # Already linked if c.layer.code != layer: logger.info( "%s - %s. Changing link layer from %s to %s", self.pop, pop, c.layer.code, layer, ) c.layer = Layer.get_by_code(layer) linked.remove(pop) else: # Unlink logger.info("Unlinking %s - %s", self.pop, pop) c.delete() # New links for pop in linked: r_level = min(level, pop.get_data("pop", "level")) // 10 logger.info("%s - %s. Linking on layer pop_links%d", self.pop, pop, r_level) self.pop.connect_genderless("links", pop, "links", type="pop_link", layer="pop_links%d" % r_level)
def fix(): for d in ObjectConnection._get_collection().find( {"data.level": { "$exists": True }}): layer = Layer.get_by_code("pop_links%d" % (d["data"]["level"] // 10)) oc = ObjectConnection.objects.filter(id=d["_id"]).first() oc.layer = layer oc.save()
def connect_genderless( self, name: str, remote_object: "Object", remote_name: str, data: Dict[str, Any] = None, type: Optional[str] = None, layer: Optional[Layer] = None, ): """ Connect two genderless connections """ lc = self.model.get_model_connection(name) if lc is None: raise ConnectionError("Local connection not found: %s" % name) name = lc.name rc = remote_object.model.get_model_connection(remote_name) if rc is None: raise ConnectionError("Remote connection not found: %s" % remote_name) remote_name = rc.name if lc.gender != "s": raise ConnectionError("Local connection '%s' must be genderless" % name) if rc.gender != "s": raise ConnectionError("Remote connection '%s' must be genderless" % remote_name) # Check for connection for c, ro, rname in self.get_genderless_connections(name): if ro.id == remote_object.id and rname == remote_name: c.data = data or {} c.save() return # Normalize layer if layer and isinstance(layer, str): layer = Layer.get_by_code(layer) # Create connection ObjectConnection( connection=[ ObjectConnectionItem(object=self, name=name), ObjectConnectionItem(object=remote_object, name=remote_name), ], data=data or {}, type=type or None, layer=layer, ).save() self.log("%s:%s -> %s:%s" % (self, name, remote_object, remote_name), system="CORE", op="CONNECT")
def set_point(self): from noc.gis.map import map # Reset previous data self.layer = None self.point = None # Get points x, y, srid = self.get_data_tuple("geopoint", ("x", "y", "srid")) if x is None or y is None: return # No point data # Get layer layer_code = self.model.get_data("geopoint", "layer") if not layer_code: return layer = Layer.get_by_code(layer_code) if not layer: return # Update actual data self.layer = layer self.point = map.get_db_point(x, y, srid=srid)
def set_point(self): from noc.gis.map import map self.layer = None self.point = None geo = self.data.get("geopoint") if not geo: return layer_code = self.model.get_data("geopoint", "layer") if not layer_code: return layer = Layer.get_by_code(layer_code) if not layer: return x = geo.get("x") y = geo.get("y") srid = geo.get("srid") if x and y: self.layer = layer self.point = map.get_db_point(x, y, srid=srid)
def fix(): layer = Layer.get_by_code("conduits") for oc in ObjectConnection.objects.filter(type="conduits"): oc.layer = layer oc.save()
def get_pop_links_layer(self, layer, x0, y0, x1, y1, srid): line = Layer.get_by_code(layer) return map.get_connection_layer(line, x0, y0, x1, y1, srid)
def get_conduits_layer(self, layer, x0, y0, x1, y1, srid): line = Layer.get_by_code("conduits") return map.get_connection_layer(line, x0, y0, x1, y1, srid)