def addOverlay(projectId: int, fileObj: IO, bounds: List[float], label: str) -> Overlay: """ :param projectId: int :param fileObj: IO :param bounds: List [minLon, minLat, maxLon, maxLat] :param label: str :return: None """ imdata = ImageService.processOverlay(fileObj) ov = Overlay() ov.label = label ov.minLon = bounds[0] ov.minLat = bounds[1] ov.maxLon = bounds[2] ov.maxLat = bounds[3] ov.project_id = projectId ov.uuid = uuid.uuid4() asset_path = os.path.join(str(make_project_asset_dir(projectId)), str(ov.uuid) + '.jpeg') ov.path = get_asset_relative_path(asset_path) imdata.original.save(asset_path, 'JPEG') imdata.thumb.save( pathlib.Path(asset_path).with_suffix(".thumb.jpeg"), "JPEG") db_session.add(ov) db_session.commit() return ov
def createVideoFeatureAsset(projectId: int, fileObj: IO, original_path: str = None) -> FeatureAsset: """ :param projectId: :param fileObj: Should be a file descriptor of a file in tmp :return: FeatureAsset """ asset_uuid = uuid.uuid4() base_filepath = make_project_asset_dir(projectId) save_path = os.path.join(base_filepath, str(asset_uuid) + '.mp4') with tempfile.TemporaryDirectory() as tmpdirname: tmp_path = os.path.join(tmpdirname, str(asset_uuid)) with open(tmp_path, 'wb') as tmp: tmp.write(fileObj.read()) encoded_path = VideoService.transcode(tmp_path) with open(encoded_path, 'rb') as enc: with open(save_path, 'wb') as f: f.write(enc.read()) # clean up the tmp file os.remove(encoded_path) fa = FeatureAsset(uuid=asset_uuid, original_path=original_path, display_path=original_path, path=get_asset_relative_path(save_path), asset_type="video") return fa
def featureAssetFromImData(projectId: int, imdata: ImageData) -> FeatureAsset: asset_uuid = uuid.uuid4() base_filepath = make_project_asset_dir(projectId) asset_path = os.path.join(base_filepath, str(asset_uuid) + '.jpeg') imdata.thumb.save( pathlib.Path(asset_path).with_suffix(".thumb.jpeg"), "JPEG") imdata.resized.save(asset_path, "JPEG") fa = FeatureAsset(uuid=asset_uuid, asset_type="image", path=get_asset_relative_path(asset_path)) return fa
def fromImage(projectId: int, fileObj: IO, metadata: Dict, original_path: str = None) -> Feature: """ Create a Point feature from a georeferenced image :param projectId: int :param fileObj: file :param metadata: dict :return: None """ imdata = ImageService.processImage(fileObj) point = Point(imdata.coordinates) f = Feature() f.project_id = projectId f.the_geom = from_shape(point, srid=4326) f.properties = metadata asset_uuid = uuid.uuid4() base_filepath = make_project_asset_dir(projectId) asset_path = os.path.join(base_filepath, str(asset_uuid) + '.jpeg') fa = FeatureAsset( uuid=asset_uuid, asset_type="image", original_path=original_path, display_path=original_path, path=get_asset_relative_path(asset_path), feature=f, ) f.assets.append(fa) thumbnail_path = os.path.join(base_filepath, str(asset_uuid) + ".thumb.jpeg") resized_image_path = os.path.join(base_filepath, str(asset_uuid) + '.jpeg') try: imdata.thumb.save(thumbnail_path, "JPEG") imdata.resized.save(resized_image_path, "JPEG") except: if os.path.exists(thumbnail_path): os.remove(thumbnail_path) if os.path.exists(resized_image_path): os.remove(resized_image_path) raise finally: fileObj.close() db_session.add(f) db_session.commit() return f
def createImageFeatureAsset(projectId: int, fileObj: IO, original_path: str = None) -> FeatureAsset: asset_uuid = uuid.uuid4() imdata = ImageService.resizeImage(fileObj) base_filepath = make_project_asset_dir(projectId) asset_path = os.path.join(base_filepath, str(asset_uuid) + '.jpeg') imdata.thumb.save( pathlib.Path(asset_path).with_suffix(".thumb.jpeg"), "JPEG") imdata.resized.save(asset_path, "JPEG") fa = FeatureAsset(uuid=asset_uuid, asset_type="image", original_path=original_path, display_path=original_path, path=get_asset_relative_path(asset_path)) return fa
def create(projectId: int, data: dict, user: User) -> PointCloud: """ Create a PointCloud for a user. :param projectId: int :param data: dict :param user: User :return: PointCloud """ point_cloud_uid = uuid.uuid4() point_cloud_path = os.path.join(make_project_asset_dir(projectId), str(point_cloud_uid)) file_point_cloud_path = os.path.join(point_cloud_path, PointCloudService.ORIGINAL_FILES_DIR) pathlib.Path(file_point_cloud_path).mkdir(parents=True, exist_ok=True) point_cloud = PointCloud(**data) point_cloud.project_id = projectId point_cloud.tenant_id = user.tenant_id point_cloud.uuid = point_cloud_uid point_cloud.path = get_asset_relative_path(point_cloud_path) db_session.add(point_cloud) db_session.commit() return point_cloud
def convert_to_potree(self, pointCloudId: int) -> None: """ Use the potree converter to convert a LAS/LAZ file to potree format :param pointCloudId: int :return: None """ from geoapi.models import Feature, FeatureAsset from geoapi.services.point_cloud import PointCloudService point_cloud = PointCloudService.get(pointCloudId) path_to_original_point_clouds = get_asset_path( point_cloud.path, PointCloudService.ORIGINAL_FILES_DIR) path_temp_processed_point_cloud_path = get_asset_path( point_cloud.path, PointCloudService.PROCESSED_DIR) input_files = [ get_asset_path(path_to_original_point_clouds, file) for file in os.listdir(path_to_original_point_clouds) if pathlib.Path(file).suffix.lstrip('.').lower() in PointCloudService.LIDAR_FILE_EXTENSIONS ] outline = get_bounding_box_2d(input_files) command = [ "PotreeConverter", "--verbose", "-i", path_to_original_point_clouds, "-o", path_temp_processed_point_cloud_path, "--overwrite", "--generate-page", "index" ] if point_cloud.conversion_parameters: command.extend(point_cloud.conversion_parameters.split()) logger.info("Processing point cloud (#{}): {}".format( pointCloudId, " ".join(command))) subprocess.run(command, check=True, capture_output=True, text=True) # Create preview viewer html (with no menu and now nsf logo) with open( os.path.join(path_temp_processed_point_cloud_path, "preview.html"), 'w+') as preview: with open( os.path.join(path_temp_processed_point_cloud_path, "index.html"), 'r') as viewer: content = viewer.read() content = re.sub(r"<div class=\"nsf_logo\"(.+?)</div>", '', content, flags=re.DOTALL) content = content.replace("viewer.toggleSidebar()", "$('.potree_menu_toggle').hide()") preview.write(content) if point_cloud.feature_id: feature = point_cloud.feature else: feature = Feature() feature.project_id = point_cloud.project_id asset_uuid = uuid.uuid4() base_filepath = make_project_asset_dir(point_cloud.project_id) asset_path = os.path.join(base_filepath, str(asset_uuid)) fa = FeatureAsset(uuid=asset_uuid, asset_type="point_cloud", path=get_asset_relative_path(asset_path), feature=feature) feature.assets.append(fa) point_cloud.feature = feature feature.the_geom = from_shape(geometries.convert_3D_2D(outline), srid=4326) point_cloud.task.status = "FINISHED" point_cloud.task.description = "" point_cloud_asset_path = get_asset_path(feature.assets[0].path) shutil.rmtree(point_cloud_asset_path, ignore_errors=True) shutil.move(path_temp_processed_point_cloud_path, point_cloud_asset_path) try: db_session.add(point_cloud) db_session.add(feature) db_session.commit() except: db_session.rollback() raise