def prepare_continent_json(self, continent_name): with open('meta/{0}.json'.format(continent_name), 'r+') as continent_file: continent_json = json.load(continent_file) for country in continent_json: Transnet.prepare_poly_country(continent_name, country) boundary = PolyParser.poly_to_polygon('../data/{0}/{1}/pfile.poly'.format(continent_name, country)) where_clause = "st_intersects(l.way, st_transform(st_geomfromtext('" + boundary.wkt + "',4269),3857))" # where_clause = "ST_Intersects(ST_GeographyFromText(s.geom_str), st_transform(st_geomfromtext('" + boundary.wkt + "',4269),4326))" # with open('../data/{0}/{1}/where_clause'.format(continent_name, country), 'w') as wfile: # wfile.write(where_clause) query = '''SELECT DISTINCT(voltage) AS voltage, count(*) AS num FROM planet_osm_line l WHERE %s GROUP BY voltage ORDER BY num DESC''' % where_clause continent_json[country]['voltages'] = self.get_voltages_from_query(query=query) continent_file.seek(0) continent_file.write(json.dumps(continent_json, indent=4)) continent_file.truncate()
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))