예제 #1
0
    def retrieve_relations(self):

        circuits = []
        sql = "SELECT parts FROM planet_osm_rels r1 WHERE ARRAY[27124619]::BIGINT[] <@ r1.parts AND hstore(r1.tags)->'voltage' ~ '110000|220000|380000' AND hstore(r1.tags)->'type'='route' AND hstore(r1.tags)->'route'='power'"
        self.cur.execute(sql)
        result = self.cur.fetchall()
        for (parts,) in result:
            relation = []
            for part in parts:
                sql = "SELECT hstore(tags)->'power' FROM planet_osm_ways WHERE id = " + str(part)
                self.cur.execute(sql)
                [(type,)] = self.cur.fetchall()
                if 'station' in type:
                    sql = "SELECT id,create_polygon(id) AS geom, hstore(tags)->'power' AS type, hstore(tags)->'name' AS name, hstore(tags)->'ref' AS ref, hstore(tags)->'voltage' AS voltage, nodes, tags, ST_Y(ST_Transform(ST_Centroid(create_polygon(id)),4326)) AS lat, ST_X(ST_Transform(ST_Centroid(create_polygon(id)),4326)) AS lon FROM planet_osm_ways WHERE id = " + str(
                        part)
                    self.cur.execute(sql)
                    [(id, geom, type, name, ref, voltage, nodes, tags, lat, lon)] = self.cur.fetchall()
                    polygon = wkb.loads(geom, hex=True)
                    relation.append(Station(id, polygon, type, name, ref, voltage, nodes, tags, lat, lon, geom))
                elif 'generator' in type or 'plant' in type:
                    sql = "SELECT id,create_polygon(id) AS geom, hstore(tags)->'power' AS type, hstore(tags)->'name' AS name, hstore(tags)->'ref' AS ref, hstore(tags)->'voltage' AS voltage, hstore(tags)->'plant:output:electricity' AS output1, hstore(tags)->'generator:output:electricity' AS output2, nodes, tags, ST_Y(ST_Transform(ST_Centroid(create_polygon(id)),4326)) AS lat, ST_X(ST_Transform(ST_Centroid(create_polygon(id)),4326)) AS lon FROM planet_osm_ways WHERE id = " + str(
                        part)
                    self.cur.execute(sql)
                    [(
                        id, geom, type, name, ref, voltage, output1, output2, nodes, tags, lat,
                        lon)] = self.cur.fetchall()
                    polygon = wkb.loads(geom, hex=True)
                    generator = Station(id, polygon, type, name, ref, voltage, nodes, tags, lat, lon, geom)
                    generator.nominal_power = Transnet.parse_power(
                        output1) if output1 is not None else Transnet.parse_power(output2)
                    relation.append(generator)
                elif 'line' in type or 'cable' in type:
                    sql = "SELECT id, create_line(id) AS geom, hstore(tags)->'power' AS type, hstore(tags)->'name' AS name, hstore(tags)->'ref' AS ref, hstore(tags)->'voltage' AS voltage, hstore(tags)->'cables' AS cables, nodes, tags, ST_Y(ST_Transform(ST_Centroid(create_line(id)),4326)) AS lat, ST_X(ST_Transform(ST_Centroid(create_line(id)),4326)) AS lon FROM planet_osm_ways WHERE id = " + str(
                        part)
                    self.cur.execute(sql)
                    [(id, geom, type, name, ref, voltage, cables, nodes, tags, lat, lon)] = self.cur.fetchall()
                    line = wkb.loads(geom, hex=True)
                    relation.append(
                        Line(id, line, type, name, ref, voltage, cables, nodes, tags, lat, lon, None, None, None, geom))
                else:
                    print('Unknown power tag ' + type)
            sorted_relation = CimWriterTest.sort_relation(relation)
            reference_line = CimWriterTest.get_reference_line(sorted_relation)
            circuits.append(Circuit(sorted_relation, reference_line.voltage, reference_line.name, reference_line.ref))
            for circuit in circuits:
                circuit.print_circuit()

            for circuit in circuits:
                circuit.print_overpass()

        print('CIM model generation started ...')
        cim_writer = CimWriter(circuits, None, None, None)
        cim_writer.publish('../results/cim')

        return
예제 #2
0
    def test_determine_load_voltage(self):
        tw1 = TransformerWinding(ratedU=380000)
        tw2 = TransformerWinding(ratedU=220000)
        tw3 = TransformerWinding(ratedU=110000)
        transformer = PowerTransformer([tw1, tw2])
        self.assertEqual(220000, CimWriter.determine_load_voltage(transformer))

        transformer = PowerTransformer([tw2])
        self.assertEqual(220000, CimWriter.determine_load_voltage(transformer))

        transformer = PowerTransformer([tw1, tw2, tw3])
        self.assertEqual(110000, CimWriter.determine_load_voltage(transformer))
예제 #3
0
    def modeling(self, country_name):

        # create dest dir
        if not exists(self.destdir):
            makedirs(self.destdir)

        root.info('Infer for %s', country_name)

        time = datetime.now()

        # build location where clause for succeeding queries
        boundary = None
        if self.poly:
            boundary = PolyParser.poly_to_polygon(self.poly)
            where_clause = "st_intersects(l.way, st_transform(st_geomfromtext('" + boundary.wkt + "',4269),3857))"
        elif self.bpoly:
            boundary = wkt.loads(self.bpoly)
            where_clause = "st_intersects(l.way, st_transform(st_geomfromtext('" + boundary.wkt + "',4269),3857))"
        else:
            where_clause = "st_distance(l.way, (select way from planet_osm_polygon where osm_id = " + str(
                self.ssid) + ")) <= 300000"

        # do inference for each voltage level
        all_circuits = []
        all_substations = dict()
        all_generators = dict()
        equipment_points = []
        length_found_lines = 0

        for voltage_level in self.voltage_levels.split('|'):
            (length_found_lines, equipment_points, generators, substations, circuits) = self.inference_for_voltage(
                voltage_level, where_clause, length_found_lines, equipment_points,
                all_substations, all_generators, boundary)
            all_generators.update(generators)
            all_substations.update(substations)
            all_circuits.extend(circuits)

        root.info('Total length of all found lines is %s meters', str(length_found_lines))
        equipments_multipoint = MultiPoint(equipment_points)
        map_centroid = equipments_multipoint.centroid
        logging.debug('Centroid lat:%lf, lon:%lf', map_centroid.x, map_centroid.y)
        all_circuits = Transnet.remove_duplicates(all_circuits)
        root.info('Inference took %s millies', str(datetime.now() - time))

        transnet_instance.export_to_json(all_circuits)

        partition_by_station_dict = None
        population_by_station_dict = None
        cities = None
        if self.load_estimation:
            root.info('Start partitioning into Voronoi-portions')
            load_estimator = LoadEstimator(all_substations, boundary)
            partition_by_station_dict, population_by_station_dict = load_estimator.partition()
            cities = load_estimator.cities

        if self.topology:
            root.info('Plot inferred transmission system topology')
            plotter = Plotter(self.voltage_levels)
            plotter.plot_topology(all_circuits, equipments_multipoint, partition_by_station_dict, cities, self.destdir)

        root.info('CSV generation started ...')
        csv_writer = CSVWriter(all_circuits)
        csv_writer.publish(self.destdir + '/csv')

        root.info('CIM model generation started ...')
        cim_writer = CimWriter(all_circuits, map_centroid, population_by_station_dict, self.voltage_levels)
        cim_writer.publish(self.destdir + '/cim')

        # for circuit in all_circuits:
        #     for line in circuit.members[1:-1]:
        #         if line.id not in self.all_lines:
        #             self.length_all += line.length
        #             self.all_lines[line.id] = line.id
        #
        # root.info('All lines length without duplicates %s', str(self.length_all / 1000))
        #
        # self.length_all = 0
        # for circuit in all_circuits:
        #     for line in circuit.members[1:-1]:
        #         self.length_all += line.length
        #
        # root.info('All lines length with duplicates %s', str(self.length_all / 1000))
        #
        # for circuit in all_circuits:
        #     sts = [circuit.members[0], circuit.members[-1]]
        #     for st in sts:
        #         if st.id not in self.all_stations:
        #             self.all_stations[st.id] = 1
        #         else:
        #             self.all_stations[st.id] += 1
        #
        # root.info('All Stations %s', str(self.all_stations))

        if validate:
            validator = InferenceValidator(self.cur)
            if boundary:
                all_stations = all_substations.copy()
                all_stations.update(all_generators)
                validator.validate2(all_circuits, all_stations, boundary, self.voltage_levels)
            else:
                validator.validate(self.ssid, all_circuits, None, self.voltage_levels)

        root.info('Took %s in total', str(datetime.now() - time))