CoolFace
Apppublic

forestaiUs/treeextraction-ndvi

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
advanced_extraction.cpython-312.pyc91 linesDownload Raw Back to __pycache__
1�

2��hI%��R�dZddlZddlZddlZddlZddlmZdd�Zdd�Zd�Z	d	d�Z3y)4z�5Advanced feature extraction using geoai-py package.6This module provides integration with the geoai-py package for more accurate7feature extraction from geospatial imagery.8�N)�shapec��	tjd|�d��tj�}tj9j
tj10j|��d}tj11j||�d��}|j||d|dddd	|d12��13}|j|ddd
d��}	|j�D|jdk7r5tjd|j�d��|jd�}nz|j�ndd	l
}	|	j|�5}14|15j�Dtjd|16j���|17j|_|jd�}d	d	d	�tj18j||�d��}|j%|d��tjdt'|��d��|S#1swY�cxYw#t$r+}tj dt#|����Yd	}~��d	}~wwxYw#t$r'}tj(dt#|�����d	}~wwxYw)a�19    Extract building footprints from a GeoTIFF image using geoai-py.20 21    Args:22        image_path (str): Path to the input GeoTIFF image23        output_folder (str): Directory to save output files24        confidence_threshold (float): Confidence threshold for detection (0.0-1.0)25        mask_threshold (float): Mask threshold for segmentation (0.0-1.0)26 27    Returns:28        str: Path to the generated GeoJSON file29    zExtracting buildings from z using geoai-pyrz_buildings.geojson�g�?��?�dNg�?)	�output_path�30batch_size�confidence_threshold�overlap�nms_iou_threshold�min_object_area�max_object_area�mask_threshold�simplify_tolerance�g333333�?gffffff�?)�gdf�min_area�angle_threshold�orthogonality_threshold�rectangularity_thresholdz	EPSG:4326zConverting GeoDataFrame from z to WGS84 (EPSG:4326)zSetting CRS from image: zError reprojecting to WGS84: z_buildings_regularized.geojson�GeoJSON)�driverzSuccessfully extracted z31 buildingsz*Error extracting buildings with geoai-py: )�logging�info�geoai�BuildingFootprintExtractor�os�path�splitext�basename�join�process_raster�regularize_buildings�crs�to_crs�rasterio�open�	Exception�warning�str�to_file�len�error)
�32image_path�
output_folderr33r�	extractor�	base_name�geojson_pathr�gdf_regularizedr&�src�e�regularized_geojson_paths
             �8C:\Users\caoya\DEV\ForestAI\utils\advanced_extraction.py�extract_buildings_from_geotiffr8
s5��?����1�*��_�M�N��4�4�6�	��G�G�$�$�R�W�W�%5�%5�j�%A�B�1�E�	��w�w�|�|�M�i�[�@R�3S�T���&�&��$��!5��!�� �)�"�'�34��$�8�8����$'�%(�9�35��	F��"�"�.�?�3F�3F�+�3U����<�_�=P�=P�<Q�Qf�g�h�"1�"8�"8��"E�� �$�$�,���]�]�:�.�#��w�w�*����'?����y�%I�J�.1�g�g��+�*9�*@�*@��*M��/�$&�7�7�<�<�
�)��Lj�?k�#l� ���� 8���K����.�s�?�/C�.D�J�O�P�(�'�!/�.���	F��O�O�;�C��F�8�D�E�E��	F�����
�
�B�3�q�6�(�K�L�
���s]�CH*�A1G3�4AG'�G3�
AH*�'G0�,G3�3	H'�<!H"�H*�"H'�'H*�*	I�3"I�Ic�z�ddlm}ddlm}|||�}||d|��}tj36j
tj37j|��d}tj38j||�d��}	t|	d�5}39tj||40�ddd�|	S#1swY|	SxYw)	a�41    Extract tree/vegetation cover from a GeoTIFF image.42    This is a placeholder for future implementation.43 44    Args:45        image_path (str): Path to the input GeoTIFF image46        output_folder (str): Directory to save output files47        confidence_threshold (float): Confidence threshold for detection (0.0-1.0)48        mask_threshold (float): Mask threshold for segmentation (0.0-1.0)49 50    Returns:51        str: Path to the generated GeoJSON file52    r��process_image_to_geojson��
process_image�trees��feature_type�original_file_pathz_trees.geojson�wN)�utils.geospatialr;�utils.image_processingr=rrrr r!r'�json�dump)r.r/r53rr;r=�processed_image_path�geojson_datar1r2�fs           r7�extract_trees_from_geotiffrJ[s��� :�4�(��]�C��+�,@�w�ku�v�L���� � ����!1�!1�*�!=�>�q�A�I��7�7�<�<�
�)��N�/K�L�L�	
�l�C�	 �A��	�	�,��"�54!���55!���s�B0�0B:c56�z�	t|d�5}tj|�}ddd�tjd|���r`d|vr\|drW|dd}d|vrKd|dvrD	|ddd	k(r|dddd}n|ddddd}tjd57|���d|vrldgd
�}t|�D]2\}}|djd|d|jdd|i�d���4tjdt|d��d��|Stjdt|d��d��|S#1swY��xYw#t58$r+}tjdt|����Yd}~��d}~wwxYw#t59$r0}tjdt|����dgd
�cYd}~Sd}~wwxYw)z�60    Convert a GeoJSON file from geoai-py to the format expected by our application.61 62    Args:63        geojson_path (str): Path to the GeoJSON file64 65    Returns:66        dict: GeoJSON data in the format expected by our application67    �rNzGeoJSON data loaded from �featuresr�geometry�coordinates�type�PolygonzFirst feature coordinates: z1Error extracting coordinates from first feature: �FeatureCollection�rPrM�Feature�68properties�id)rPrNrUz,Converted GeoJSON to FeatureCollection with z	 featuresz1GeoJSON already in FeatureCollection format with z!Error converting GeoJSON format: )
r'rE�loadrrr(r)r*�	enumerate�append�getr,r-)	r2rIrH�
first_feature�coordsr5�converted_geojson�i�features	         r7�geojson_to_app_formatr`zs���-=�
�,��
$���9�9�Q�<�L�%�	���0���?�@��J�,�6�<�69�;S�(��4�Q�7�M��]�*�}�
�j�@Y�/Y�b�$�Z�0��8�I�E�!.�z�!:�=�!I�!�!L�Q�!O��!.�z�!:�=�!I�!�!L�Q�!O�PQ�!R���L�L�#>�v�h�!G�H�70�\�)�,��!��(��5�71��7�!�*�-�4�4�%� '�72� 3�")�+�+�l�T�1�I�"F�6��6�
�L�L�G��L]�^h�Li�Hj�Gk�kt�u�v�$�$�	���H��\�Zd�Me�If�Hg�gp�q�r���M%�
$��!�b��O�O�&W�X[�\]�X^�W_�$`�a�a��b��4�=��
�
�9�#�a�&��B�C�+��<�<��=�se�F�D=�>F�#AE73�&A/F�&F�=E�F�74	E>�!E9�4F�9E>�>F�	F:�75%F5�/F:�5F:c���	|j�dk(r
t||�}n�|j�dk(s|j�dk(r
t||�}n�ddlm}ddlm}|||�}||||��}tjjtjj|��d}tjj||�d|�d	��}t|d76�5}	tj||	�ddd�||d<|St|�}77||78d<|79S#1swY�"xYw#t $r0}t#j$d
t'|����dgd�cYd}~Sd}~wwxYw)a80    Extract features from a GeoTIFF image based on the feature type.81 82    Args:83        image_path (str): Path to the input GeoTIFF image84        output_folder (str): Directory to save output files85        feature_type (str): Type of features to extract ("buildings", "trees", "water", "roads")86 87    Returns:88        dict: GeoJSON data in the format expected by our application89    �	buildingsr>�90vegetationrr:r<r?�_z.geojsonrBNr@zError extracting features: rRrS)�lowerr8rJrCr;rDr=rrrr r!r'rErFr`r(rr-r*)r.r/r@r2r;r=rGrHr1rI�resultr5s            r7�extract_features_from_geotiffrg�sX��'=�����;�.�9�*�m�T�L�
�
�
�
!�W�
,��0B�0B�0D��0T�5�j�-�P�L�
B�<�#0��]�#K� �3�4H�Wc�yC�D�L����(�(����)9�)9�*�)E�F�q�I�I��7�7�<�<�
�)��A�l�^�S[�7\�]�L��l�C�(�A��	�	�,��*�)�,8�L��(� ��'�|�4��".��~���
�)�(��"�=��
�
�3�C��F�8�<�=�+��<�<��=�s<�C$D*�&D�=D*�D*�D'�#D*�*	E#�3%E�E#�E#)rr)rb)�__doc__rrrrE�shapely.geometryrr8rJr`rg��r7�<module>rls2���91����"�L�\�>7=�r3=rk