geoai-py 0.5.5__py2.py3-none-any.whl → 0.6.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
geoai/__init__.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  __author__ = """Qiusheng Wu"""
4
4
  __email__ = "giswqs@gmail.com"
5
- __version__ = "0.5.5"
5
+ __version__ = "0.6.0"
6
6
 
7
7
 
8
8
  import os
geoai/classify.py CHANGED
@@ -1,6 +1,7 @@
1
1
  """The module for training semantic segmentation models for classifying remote sensing imagery."""
2
2
 
3
3
  import os
4
+
4
5
  import numpy as np
5
6
 
6
7
 
@@ -110,23 +111,24 @@ def train_classifier(
110
111
  Returns:
111
112
  object: Trained SemanticSegmentationTask model.
112
113
  """
114
+ import multiprocessing as mp
115
+ import timeit
116
+
117
+ import albumentations as A
113
118
  import lightning.pytorch as pl
119
+ import torch
120
+ from lightning.pytorch.callbacks import ModelCheckpoint
121
+ from lightning.pytorch.loggers import CSVLogger
114
122
  from torch.utils.data import DataLoader
115
- from torchgeo.datasets import stack_samples, RasterDataset
123
+ from torchgeo.datamodules import GeoDataModule
124
+ from torchgeo.datasets import RasterDataset, stack_samples
116
125
  from torchgeo.datasets.splits import random_bbox_assignment
117
126
  from torchgeo.samplers import (
118
- RandomGeoSampler,
119
- RandomBatchGeoSampler,
120
127
  GridGeoSampler,
128
+ RandomBatchGeoSampler,
129
+ RandomGeoSampler,
121
130
  )
122
- import torch
123
- import multiprocessing as mp
124
- import timeit
125
- import albumentations as A
126
- from torchgeo.datamodules import GeoDataModule
127
131
  from torchgeo.trainers import SemanticSegmentationTask
128
- from lightning.pytorch.callbacks import ModelCheckpoint
129
- from lightning.pytorch.loggers import CSVLogger
130
132
 
131
133
  # Create a wrapper class for albumentations to work with TorchGeo format
132
134
  class AlbumentationsWrapper:
@@ -376,21 +378,19 @@ def _classify_image(
376
378
  Returns:
377
379
  str: Path to the saved classified image.
378
380
  """
379
- import numpy as np
380
381
  import timeit
381
- from tqdm import tqdm
382
382
 
383
+ import numpy as np
384
+ import rasterio
383
385
  import torch
386
+ from rasterio.io import MemoryFile
387
+ from rasterio.merge import merge
388
+ from rasterio.transform import from_origin
384
389
  from torch.utils.data import DataLoader
385
-
386
390
  from torchgeo.datasets import RasterDataset, stack_samples
387
391
  from torchgeo.samplers import GridGeoSampler
388
392
  from torchgeo.trainers import SemanticSegmentationTask
389
-
390
- import rasterio
391
- from rasterio.transform import from_origin
392
- from rasterio.io import MemoryFile
393
- from rasterio.merge import merge
393
+ from tqdm import tqdm
394
394
 
395
395
  # Set default output path if not provided
396
396
  if output_path is None:
@@ -619,14 +619,12 @@ def classify_image(
619
619
  str: Path to the saved classified image.
620
620
  """
621
621
  import timeit
622
-
623
- import torch
624
- from torchgeo.trainers import SemanticSegmentationTask
622
+ import warnings
625
623
 
626
624
  import rasterio
627
-
628
- import warnings
625
+ import torch
629
626
  from rasterio.errors import NotGeoreferencedWarning
627
+ from torchgeo.trainers import SemanticSegmentationTask
630
628
 
631
629
  # Disable specific GDAL/rasterio warnings
632
630
  warnings.filterwarnings("ignore", category=UserWarning, module="rasterio._.*")
@@ -863,9 +861,10 @@ def classify_images(
863
861
  list: List of paths to the saved classified images.
864
862
  """
865
863
  # Import required libraries
866
- from tqdm import tqdm
867
864
  import glob
868
865
 
866
+ from tqdm import tqdm
867
+
869
868
  # Process directory input
870
869
  if isinstance(image_paths, str) and os.path.isdir(image_paths):
871
870
  # Set default output directory if not provided
geoai/download.py CHANGED
@@ -1135,7 +1135,37 @@ def view_pc_item(
1135
1135
  map_args=None,
1136
1136
  **kwargs,
1137
1137
  ):
1138
+ """
1139
+ Visualize a STAC item from the Planetary Computer on an interactive map.
1140
+
1141
+ This function allows users to display a STAC item on a map using either the
1142
+ `folium` or `ipyleaflet` backend. It supports adding basemaps, configuring
1143
+ map options, and customizing the visualization of the STAC item.
1144
+
1145
+ Args:
1146
+ url (str, optional): URL of the STAC item to visualize. Defaults to None.
1147
+ collection (str, optional): The collection ID of the STAC item. Defaults to None.
1148
+ item (str or pystac.Item, optional): The STAC item or its ID. Defaults to None.
1149
+ assets (list, optional): List of asset keys to visualize. Defaults to None.
1150
+ bands (list, optional): List of specific bands to visualize. Defaults to None.
1151
+ titiler_endpoint (str, optional): URL of the Titiler endpoint for rendering. Defaults to None.
1152
+ name (str, optional): Name of the layer to display on the map. Defaults to "STAC Item".
1153
+ attribution (str, optional): Attribution text for the layer. Defaults to "Planetary Computer".
1154
+ opacity (float, optional): Opacity of the layer (0.0 to 1.0). Defaults to 1.0.
1155
+ shown (bool, optional): Whether the layer is visible by default. Defaults to True.
1156
+ fit_bounds (bool, optional): Whether to fit the map bounds to the layer. Defaults to True.
1157
+ layer_index (int, optional): Index of the layer in the map's layer stack. Defaults to None.
1158
+ backend (str, optional): Map backend to use ('folium' or 'ipyleaflet'). Defaults to "folium".
1159
+ basemap (str, optional): Name of the basemap to add to the map. Defaults to None.
1160
+ map_args (dict, optional): Additional arguments for configuring the map. Defaults to None.
1161
+ **kwargs: Additional keyword arguments passed to the `add_stac_layer` method.
1138
1162
 
1163
+ Returns:
1164
+ leafmap.Map: An interactive map with the STAC item visualized.
1165
+
1166
+ Raises:
1167
+ ValueError: If an unsupported backend is specified.
1168
+ """
1139
1169
  if backend == "folium":
1140
1170
  import leafmap.foliumap as leafmap
1141
1171
 
@@ -1181,3 +1211,103 @@ def view_pc_item(
1181
1211
  **kwargs,
1182
1212
  )
1183
1213
  return m
1214
+
1215
+
1216
+ def view_pc_items(
1217
+ urls=None,
1218
+ collection=None,
1219
+ items=None,
1220
+ assets=None,
1221
+ bands=None,
1222
+ titiler_endpoint=None,
1223
+ attribution="Planetary Computer",
1224
+ opacity=1.0,
1225
+ shown=True,
1226
+ fit_bounds=True,
1227
+ layer_index=None,
1228
+ backend="folium",
1229
+ basemap=None,
1230
+ map_args=None,
1231
+ **kwargs,
1232
+ ):
1233
+ """
1234
+ Visualize multiple STAC items from the Planetary Computer on an interactive map.
1235
+
1236
+ This function allows users to display multiple STAC items on a map using either
1237
+ the `folium` or `ipyleaflet` backend. It supports adding basemaps, configuring
1238
+ map options, and customizing the visualization of the STAC items.
1239
+
1240
+ Args:
1241
+ urls (list, optional): List of URLs of the STAC items to visualize. Defaults to None.
1242
+ collection (str, optional): The collection ID of the STAC items. Defaults to None.
1243
+ items (list or str, optional): List of STAC items or their IDs. Defaults to None.
1244
+ assets (list, optional): List of asset keys to visualize. Defaults to None.
1245
+ bands (list, optional): List of specific bands to visualize. Defaults to None.
1246
+ titiler_endpoint (str, optional): URL of the Titiler endpoint for rendering. Defaults to None.
1247
+ attribution (str, optional): Attribution text for the layers. Defaults to "Planetary Computer".
1248
+ opacity (float, optional): Opacity of the layers (0.0 to 1.0). Defaults to 1.0.
1249
+ shown (bool, optional): Whether the layers are visible by default. Defaults to True.
1250
+ fit_bounds (bool, optional): Whether to fit the map bounds to the layers. Defaults to True.
1251
+ layer_index (int, optional): Index of the layers in the map's layer stack. Defaults to None.
1252
+ backend (str, optional): Map backend to use ('folium' or 'ipyleaflet'). Defaults to "folium".
1253
+ basemap (str, optional): Name of the basemap to add to the map. Defaults to None.
1254
+ map_args (dict, optional): Additional arguments for configuring the map. Defaults to None.
1255
+ **kwargs: Additional keyword arguments passed to the `add_stac_layer` method.
1256
+
1257
+ Returns:
1258
+ leafmap.Map: An interactive map with the STAC items visualized.
1259
+
1260
+ Raises:
1261
+ ValueError: If an unsupported backend is specified.
1262
+ """
1263
+ if backend == "folium":
1264
+ import leafmap.foliumap as leafmap
1265
+
1266
+ elif backend == "ipyleaflet":
1267
+ import leafmap.leafmap as leafmap
1268
+
1269
+ else:
1270
+ raise ValueError(
1271
+ f"Unsupported backend: {backend}. Supported backends are 'folium' and 'ipyleaflet'."
1272
+ )
1273
+
1274
+ if map_args is None:
1275
+ map_args = {}
1276
+
1277
+ if "draw_control" not in map_args:
1278
+ map_args["draw_control"] = False
1279
+
1280
+ if urls is not None:
1281
+ items = [pystac.Item.from_file(url) for url in urls]
1282
+
1283
+ if isinstance(items, str):
1284
+ items = [pystac.Item.from_file(items)]
1285
+ m = leafmap.Map(**map_args)
1286
+
1287
+ if basemap is not None:
1288
+ m.add_basemap(basemap)
1289
+ if isinstance(items, list):
1290
+
1291
+ for item in items:
1292
+
1293
+ if isinstance(item, pystac.Item):
1294
+ collection = item.collection_id
1295
+ if assets is None:
1296
+ assets = [list(item.assets.keys())[0]]
1297
+ item = item.id
1298
+
1299
+ m.add_stac_layer(
1300
+ collection=collection,
1301
+ item=item,
1302
+ assets=assets,
1303
+ bands=bands,
1304
+ titiler_endpoint=titiler_endpoint,
1305
+ name=item,
1306
+ attribution=attribution,
1307
+ opacity=opacity,
1308
+ shown=shown,
1309
+ fit_bounds=fit_bounds,
1310
+ layer_index=layer_index,
1311
+ **kwargs,
1312
+ )
1313
+ return m
geoai/extract.py CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # Standard Library
4
4
  import os
5
+ import time
5
6
 
6
7
  # Third-Party Libraries
7
8
  import cv2
@@ -19,7 +20,6 @@ from torchvision.models.detection import (
19
20
  maskrcnn_resnet50_fpn,
20
21
  )
21
22
  from tqdm import tqdm
22
- import time
23
23
 
24
24
  # Local Imports
25
25
  from .utils import get_raster_stats
geoai/geoai.py CHANGED
@@ -7,20 +7,21 @@ logging.getLogger("maplibre").setLevel(logging.ERROR)
7
7
  import leafmap
8
8
  import leafmap.maplibregl as maplibregl
9
9
 
10
+ from .classify import classify_image, classify_images, train_classifier
10
11
  from .download import (
11
12
  download_naip,
12
13
  download_overture_buildings,
13
14
  download_pc_stac_item,
14
- get_overture_data,
15
15
  extract_building_stats,
16
+ get_overture_data,
16
17
  pc_collection_list,
17
18
  pc_item_asset_list,
18
- pc_stac_search,
19
19
  pc_stac_download,
20
+ pc_stac_search,
20
21
  read_pc_item_asset,
21
22
  view_pc_item,
23
+ view_pc_items,
22
24
  )
23
- from .classify import train_classifier, classify_image, classify_images
24
25
  from .extract import *
25
26
  from .hf import *
26
27
  from .segment import *