def receive_energyasset_info(shapefile_energyasset_list): with self.flask_app.app_context(): print("Received shpcvrt_receive_energyasset_info") esh = get_handler() active_es_id = get_session('active_es_id') es = esh.get_energy_system(active_es_id) area = es.instance[0].area for shapefile_energyasset in shapefile_energyasset_list: energy_asset = shapefile_energyasset['energy_asset'] shapefile_name = shapefile_energyasset['shapefile_name'] zipfile_row = shapefile_energyasset['zipfile_row'] # zipfile_name = self.zipfiles[int(zipfile_row)] # directory = get_session('shapefile_dir' + zipfile_row) shapefile_name_base = os.path.splitext(shapefile_name)[0] print(shapefile_name_base + ' --> ' + energy_asset) if energy_asset != 'ignore': basename = os.path.basename(shapefile_name_base) subarea = esdl.Area(id=str(uuid4()), name=basename) area.area.append(subarea) self.process_shapefile(subarea, shapefile_name_base, energy_asset) # update uuid_dict recursively for the main area (could take long?) esh.add_object_to_dict(es_id=active_es_id, esdl_object=area, recursive=True) self.executor.submit(process_energy_system, esh=esh, filename='test', force_update_es_id=active_es_id)
def create_empty_energy_system(self, name, es_description, inst_title, area_title, esdlVersion=None): es_id = str(uuid4()) self.energy_system = esdl.EnergySystem(id=es_id, name=name, description=es_description, esdlVersion=esdlVersion) uri = StringURI('empty_energysystem.esdl') self.resource = self.rset.create_resource(uri) # add the current energy system self.resource.append(self.energy_system) self.esid_uri_dict[self.energy_system.id] = uri.normalize() instance = esdl.Instance(id=str(uuid4()), name=inst_title) self.energy_system.instance.append(instance) # TODO: check if this (adding scope) solves error???? area = esdl.Area(id=str(uuid4()), name=area_title, scope=esdl.AreaScopeEnum.from_string('UNDEFINED')) instance.area = area # add generated id's to uuid dict self.add_object_to_dict(es_id, self.energy_system) self.add_object_to_dict(es_id, instance) self.add_object_to_dict(es_id, area) return self.energy_system
def merge(self: esdl.Area, other: esdl.Area, parent=None, reference=None) -> esdl.Area: if _options['forceCombineMainArea']: print("- Force merging main area's {} and {} into one.".format( self.name, other.name)) return self elif self.id == other.id: print("- merge {}: {} with {}: both equal".format( self.eClass.name, self.name, other.name)) # return self and handle sub containments return self else: if self.containingArea is None: # this is the main area, as it does not have a containing Area print("- Merging main areas into a merged area with two subAreas") newarea = esdl.Area(id=str(uuid4()), name="Merged Area") newarea.area.append(self) newarea.area.append(other.deepcopy()) # todo update references e.g. Carriers and Sectors return newarea #parent.eSet(reference, newarea) else: print("- Areas are different, merging: TODO") # different areas: add both return _compareAndMerge(self, other)
def add_geometries_to_esdl(self, esh, es_id, area_list): with self.flask_app.app_context(): es_edit = esh.get_energy_system(es_id) instance = es_edit.instance area = instance[0].area for ibis_area in area_list: new_area = esdl.Area(id=str(uuid4()), name=ibis_area['name']) new_area.scope = esdl.AreaScopeEnum.from_string("UNDEFINED") boundary = ibis_area if boundary: geometry = ESDLGeometry.create_geometry_from_geom( boundary['geom']) new_area.geometry = geometry area.area.append(new_area) esh.add_object_to_dict(es_id, new_area)
def to_esdl(self, area, sf, transformer, energy_asset): i = 0 for shapeRecord in sf.shapeRecords(): i += 1 if shapeRecord.shape.shapeType == shapefile.POLYLINE: print('{} {} = {}'.format(shapeRecord.shape.shapeTypeName, shapeRecord.record[0], shapeRecord.shape.points)) pipe = esdl.Pipe(name=area.name + '-Pipe' + str(i), id=str(uuid4())) line = esdl.Line() d = 0.0 prevlat, prevlon = None, None for point in shapeRecord.shape.points: lon, lat = transformer.transform(point[0], point[1]) p = esdl.Point(lat=lat, lon=lon) line.point.append(p) if prevlat is not None: d = d + distance((prevlat, prevlon), (lat, lon)) prevlat, prevlon = lat, lon pipe.length = d * 1000 # in m instead of km # diameter was put in mm! pipe.innerDiameter = float( self.get_recordproperty(shapeRecord.record, inner_diameter_keys, 0.0)) / 1000 pipe.outerDiameter = float( self.get_recordproperty(shapeRecord.record, outer_diameter_keys, 0.0)) / 1000 pipe.geometry = line inport = esdl.InPort(id=str(uuid4()), name='InPort') outport = esdl.OutPort(id=str(uuid4()), name='OutPort') pipe.port.extend((inport, outport)) area.asset.append(pipe) if shapeRecord.shape.shapeType == shapefile.POINT: # probably an asset if energy_asset == "type_record": esdl_type = self.get_type(shapeRecord.record) esdl_object = esdl_type(name=self.get_name( shapeRecord.record), id=str(uuid4())) else: module = importlib.import_module('esdl.esdl') class_ = getattr(module, energy_asset) esdl_object = class_() esdl_object.id = str(uuid4()) esdl_object.name = self.get_name(shapeRecord.record) # instance = esdl.GenericProducer(name=get_name(shapeRecord.record), id=str(uuid4())) point = shapeRecord.shape.points[0] lon, lat = transformer.transform(point[0], point[1]) p = esdl.Point(lat=lat, lon=lon) esdl_object.geometry = p area.asset.append(esdl_object) inport = esdl.InPort(id=str(uuid4()), name='InPort') outport = esdl.OutPort(id=str(uuid4()), name='OutPort') esdl_object.port.extend((inport, outport)) if shapeRecord.shape.shapeType == shapefile.POLYGON \ or shapeRecord.shape.shapeType == shapefile.POLYGONM \ or shapeRecord.shape.shapeType == shapefile.POLYGONZ: # If it's a polygon, polygonm or polygonz, we ignore measures and z-coordinates for now # we also ignore holes and multipolygons if energy_asset == "type_record": esdl_type = self.get_type(shapeRecord.record) esdl_object = esdl_type(name=self.get_name( shapeRecord.record), id=str(uuid4())) elif energy_asset == "esdl_area": esdl_object = esdl.Area(id=str(uuid4()), name=self.get_name( shapeRecord.record)) else: module = importlib.import_module('esdl.esdl') class_ = getattr(module, energy_asset) esdl_object = class_() esdl_object.id = str(uuid4()) esdl_object.name = self.get_name(shapeRecord.record) # This function only supports polygons without holes (so far). parts_len = len(shapeRecord.shape.parts) print(parts_len) if parts_len > 1: multi_polygon = esdl.MultiPolygon() point_idx = 0 for pol in range(0, parts_len): if parts_len == 1: # If only one polygon, length_of_this_polygon = len(shapeRecord.shape.points) else: if pol == parts_len - 1: length_of_this_polygon = len( shapeRecord.shape.points ) - shapeRecord.shape.parts[pol] else: length_of_this_polygon = shapeRecord.shape.parts[ pol + 1] print('- {}'.format(length_of_this_polygon)) exterior = esdl.SubPolygon() polygon = esdl.Polygon(exterior=exterior) for pi in range(0, length_of_this_polygon): point = shapeRecord.shape.points[point_idx] lon, lat = transformer.transform(point[0], point[1]) p = esdl.Point(lat=lat, lon=lon) exterior.point.append(p) point_idx += 1 if parts_len > 1: multi_polygon.polygon.append(polygon) if parts_len > 1: esdl_object.geometry = multi_polygon else: esdl_object.geometry = polygon if energy_asset == "esdl_area": area.area.append(esdl_object) else: area.asset.append(esdl_object) inport = esdl.InPort(id=str(uuid4()), name='InPort') outport = esdl.OutPort(id=str(uuid4()), name='OutPort') esdl_object.port.extend((inport, outport))
def get_boundary_info(info): print('get_boundary_info:') print(info) user = get_session('user-email') user_settings = self.get_user_settings(user) boundaries_year = user_settings['boundaries_year'] shape_dictionary = get_session('shape_dictionary') if not shape_dictionary: shape_dictionary = {} identifier = info["identifier"] toparea_name = info["toparea_name"] scope = info["scope"] subscope_enabled = info["subscope_enabled"] subscope = info["subscope"] select_subareas = info["select_subareas"] selected_subareas = info["selected_subareas"] initialize_ES = info["initialize_ES"] add_boundary_to_ESDL = info["add_boundary_to_ESDL"] if not is_valid_boundary_id(identifier): send_alert( "Not a valid identifier. Try identifiers like PV27 (Noord-Holland) or GM0060 (Ameland)" ) return active_es_id = get_session('active_es_id') esh = get_handler() es_edit = esh.get_energy_system(es_id=active_es_id) instance = es_edit.instance area = instance[0].area area_list = [] boundary = None if subscope_enabled: self.__preload_subboundaries_in_cache( boundaries_year, esdl.AreaScopeEnum.from_string(str.upper(scope)), esdl.AreaScopeEnum.from_string(str.upper(subscope)), str.upper(identifier)) else: boundary = self.get_boundary_from_service( boundaries_year, esdl.AreaScopeEnum.from_string(str.upper(scope)), str.upper(identifier)) if boundary: geom = boundary['geom'] # geometry = ESDLGeometry.create_geometry_from_geom() shape = Shape.parse_geojson_geometry(boundary['geom']) if shape: shape_dictionary[identifier] = shape for i in range(0, len(geom['coordinates'])): if len(geom['coordinates']) > 1: area_id_number = " ({} of {})".format( i + 1, len(geom['coordinates'])) else: area_id_number = "" area_list.append({ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": geom['coordinates'][i] }, "properties": { "id": area.id + area_id_number, "name": area.name, "KPIs": [] } }) if initialize_ES: # change ID, name and scope of ES if select_subareas: area.id = "part of " + identifier else: area.id = identifier # Area ID has changed, re-add to dictionairy esh.add_object_to_dict(active_es_id, area) area.scope = esdl.AreaScopeEnum.from_string(str.upper(scope)) area.name = toparea_name if add_boundary_to_ESDL: # returns boundary: { type: '', boundary: [[[[ ... ]]]] } (multipolygon in RD) if not boundary: # check if already requested boundary = self.get_boundary_from_service( boundaries_year, esdl.AreaScopeEnum.from_string(str.upper(scope)), str.upper(identifier)) if boundary: geometry = ESDLGeometry.create_geometry_from_geom( boundary['geom']) area.geometry = geometry shape = Shape.parse_geojson_geometry(boundary['geom']) if shape: shape_dictionary[identifier] = shape # boundary = get_boundary_from_service(area_scope, area_id) # if boundary: # emit('area_boundary', {'info-type': 'MP-RD', 'crs': 'RD', 'boundary': boundary}) if subscope_enabled: boundaries = self.__get_subboundaries_from_service( boundaries_year, esdl.AreaScopeEnum.from_string(str.upper(scope)), esdl.AreaScopeEnum.from_string(str.upper(subscope)), str.upper(identifier)) # result (boundaries) is an ARRAY of: # {'code': 'BU00140500', 'geom': '{"type":"MultiPolygon","bbox":[...],"coordinates":[[[[6.583651,53.209594], # [6.58477,...,53.208816],[6.583651,53.209594]]]]}'} if not boundaries: send_alert( 'Error processing boundary information or no boundary information returned' ) for boundary in boundaries: geom = None try: # geom = json.loads(boundary["geom"]) geom = boundary["geom"] shape = Shape.parse_geojson_geometry(boundary['geom']) if shape: shape_dictionary[boundary['code']] = shape except Exception as e: print( 'Error parsing JSON from GEIS boundary service: ' + str(e)) if geom: skip_subarea = False if select_subareas and selected_subareas and boundary[ "code"] not in selected_subareas: skip_subarea = True if not skip_subarea: sub_area_id = boundary["code"] sub_area_name = boundary["name"] if initialize_ES: sub_area = esdl.Area() sub_area.id = sub_area_id sub_area.name = sub_area_name sub_area.scope = esdl.AreaScopeEnum.from_string( str.upper(subscope)) if add_boundary_to_ESDL: geometry = ESDLGeometry.create_geometry_from_geom( geom) sub_area.geometry = geometry area.area.append(sub_area) esh.add_object_to_dict(active_es_id, sub_area) for i in range(0, len(geom['coordinates'])): if len(geom['coordinates']) > 1: area_id_number = " ({} of {})".format( i + 1, len(geom['coordinates'])) else: area_id_number = "" area_list.append({ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": geom['coordinates'][i] }, "properties": { "id": sub_area_id + area_id_number, "name": sub_area_name, "KPIs": [] } }) set_session('shape_dictionary', shape_dictionary) emit('geojson', {"layer": "area_layer", "geojson": area_list}) print('Ready processing boundary information')