Example #1
0
 def insert(entity: Entity, data: dict[str, Any]) -> None:
     for shape in ['point', 'line', 'polygon']:
         if shape not in data or not data[shape]:
             continue  # pragma: no cover
         for item in json.loads(data[shape]):
             if not item['geometry']['coordinates'] \
                     or item['geometry']['coordinates'] == [[]]:
                 continue  # pragma: no cover
             if item['properties']['shapeType'] != 'centerpoint' \
                     and not Db.test_geom(json.dumps(item['geometry'])):
                 raise InvalidGeomException
             Db.insert(shape='linestring' if shape == 'line' else shape,
                       data={
                           'entity_id':
                           entity.id,
                           'name':
                           sanitize(item['properties']['name'], 'text'),
                           'description':
                           sanitize(item['properties']['description'],
                                    'text'),
                           'type':
                           item['properties']['shapeType'],
                           'geojson':
                           json.dumps(item['geometry'])
                       })
Example #2
0
 def insert(entity: Entity, form: FlaskForm) -> None:
     for shape in ['point', 'line', 'polygon']:
         data = getattr(form, 'gis_' + shape + 's').data
         if not data:
             continue  # pragma: no cover
         for item in json.loads(data):
             if not item['geometry']['coordinates'] \
                     or item['geometry']['coordinates'] == [[]]:
                 continue  # pragma: no cover
             if item['properties']['shapeType'] != 'centerpoint':
                 Db.test_geom(json.dumps(item['geometry']))
             Db.insert(shape='linestring' if shape == 'line' else shape,
                       data={
                           'entity_id':
                           entity.id,
                           'name':
                           sanitize(item['properties']['name'], 'text'),
                           'description':
                           sanitize(item['properties']['description'],
                                    'text'),
                           'type':
                           item['properties']['shapeType'],
                           'geojson':
                           json.dumps(item['geometry'])
                       })
Example #3
0
 def insert_import(entity: Entity, location: Entity, project: Project,
                   easting: float, northing: float) -> None:
     Db.insert_import({
         'entity_id':
         location.id,
         'description':
         f"Imported centerpoint of {sanitize(entity.name, 'text')} "
         f"from the {sanitize(project.name, 'text')} project",
         'geojson':
         f'{{"type":"Point", "coordinates": [{easting},{northing}]}}'
     })
Example #4
0
 def insert_import(entity: Entity,
                   location: Entity,
                   project: Project,
                   easting: float,
                   northing: float) -> None:
     Db.insert_import({
         'entity_id': location.id,
         'description': 'Imported centerpoint of {name} from the {project} project'.format(
             name=sanitize(entity.name, 'text'),
             project=sanitize(project.name, 'text')),
         'geojson': '''{{"type":"Point", "coordinates": [{easting},{northing}]}}'''.format(
             easting=easting, northing=northing)})
Example #5
0
    def get_all(objects: Optional[list[Entity]] = None,
                structure: Optional[dict[str, Any]] = None) -> dict[str, Any]:

        if not objects:
            objects = []
        all_: dict[str, list[Any]] = {
            'point': [],
            'linestring': [],
            'polygon': []
        }
        extra: dict[str, list[Any]] = {
            'supers': [],
            'subs': [],
            'siblings': []
        }
        selected: dict[str, list[Any]] = {
            'point': [],
            'linestring': [],
            'polygon': [],
            'polygon_point': []
        }

        # Include GIS of subunits which would be otherwise omitted
        subunit_ids = [
            subunit.id for subunit in structure['subunits']] \
            if structure else []
        sibling_ids = [
            sibling.id for sibling in structure['siblings']] \
            if structure else []
        extra_ids = [0]
        if structure:
            extra_ids = [
                objects[0].id if objects else 0] \
                + [structure['super_id']] \
                + subunit_ids \
                + sibling_ids
        object_ids = [x.id for x in objects] if objects else []

        for shape in ['point', 'polygon', 'linestring']:
            place_root = Type.get_hierarchy('Place')
            for row in Db.get_by_shape(shape, extra_ids):
                description = row['description'].replace('"', '\"') \
                    if row['description'] else ''
                object_desc = row['object_desc'].replace('"', '\"') \
                    if row['object_desc'] else ''
                item = {
                    'type': 'Feature',
                    'geometry': json.loads(row['geojson']),
                    'properties': {
                        'objectId': row['object_id'],
                        'objectName': row['object_name'].replace('"', '\"'),
                        'objectDescription': object_desc,
                        'id': row['id'],
                        'name':
                        row['name'].replace('"', '\"') if row['name'] else '',
                        'description': description,
                        'shapeType': row['type']
                    }
                }
                if 'types' in row and row['types']:
                    type_ids = ast.literal_eval(f"[{row['types']}]")
                    for type_id in list(set(type_ids)):
                        type_ = g.types[type_id]
                        if type_.root and type_.root[0] == place_root.id:
                            item['properties']['objectType'] = \
                                type_.name.replace('"', '\"')
                            break
                if structure and row['object_id'] == structure['super_id']:
                    extra['supers'].append(item)
                elif row['object_id'] in object_ids:
                    selected[shape].append(item)
                elif row['object_id'] in subunit_ids:  # pragma no cover
                    extra['subs'].append(item)
                elif row['object_id'] in sibling_ids:  # pragma no cover
                    extra['siblings'].append(item)
                else:
                    all_[shape].append(item)
                if 'polygon_point' in row:
                    polygon_point_item = dict(item)  # Make a copy
                    polygon_point_item['geometry'] = json.loads(
                        row['polygon_point'])
                    if row['object_id'] in object_ids:
                        selected['polygon_point'].append(polygon_point_item)
                    elif row['object_id'] and structure and \
                            row['object_id'] == structure['super_id']:
                        extra['supers'].append(polygon_point_item)
                    elif row['object_id'] in subunit_ids:  # pragma no cover
                        extra['subs'].append(polygon_point_item)
                    elif row['object_id'] in sibling_ids:  # pragma no cover
                        extra['siblings'].append(polygon_point_item)
                    else:
                        all_['point'].append(polygon_point_item)
        return {
            'gisPointAll':
            json.dumps(all_['point']),
            'gisPointSelected':
            json.dumps(selected['point']),
            'gisPointSupers':
            json.dumps(extra['supers']),
            'gisPointSubs':
            json.dumps(extra['subs']),
            'gisPointSibling':
            json.dumps(extra['siblings']),
            'gisLineAll':
            json.dumps(all_['linestring']),
            'gisLineSelected':
            json.dumps(selected['linestring']),
            'gisPolygonAll':
            json.dumps(all_['polygon']),
            'gisPolygonSelected':
            json.dumps(selected['polygon']),
            'gisPolygonPointSelected':
            json.dumps(selected['polygon_point']),
            'gisAllSelected':
            json.dumps(selected['polygon'] + selected['linestring'] +
                       selected['point'])
        }
Example #6
0
 def delete_by_entity(entity: Entity) -> None:
     Db.delete_by_entity_id(entity.id)
Example #7
0
 def get_by_id(id_: int) -> list[dict[str, Any]]:
     return Db.get_by_id(id_)
Example #8
0
 def get_by_id(id_: int) -> List[Dict[str, Any]]:
     return Db.get_by_id(id_)
Example #9
0
 def add_example_geom(location: Entity) -> None:
     # Used for tests until model is decoupled from forms
     Db.add_example_geom(location.id)