UrbanDataSet
Dataset class for urban imagery inference using MLLMs.
Source code in urbanworm/UrbanDataSet.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
|
LLM_chat(model='gemma3:12b', system=None, prompt=None, img=None, temp=None, top_k=None, top_p=None)
¶
Chat with the LLM model with a list of images.
Depending on the number of images provided, the method will: - Return a single Response object if only one image is provided. - Return a list of QnA objects if multiple images are provided (e.g., aerial and street views).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model name. |
'gemma3:12b'
|
system
|
str
|
The system message guiding the LLM. |
None
|
prompt
|
str
|
The user prompt to the LLM. |
None
|
img
|
list[str]
|
A list of image paths. |
None
|
temp
|
float
|
Temperature parameter for response randomness. |
None
|
top_k
|
float
|
Top-K sampling filter. |
None
|
top_p
|
float
|
Top-P (nucleus) sampling filter. |
None
|
Returns:
Type | Description |
---|---|
Union[Response, list[QnA]]
|
Union[Response, list[QnA]]: A Response object if a single reply is generated, |
Union[Response, list[QnA]]
|
or a list of QnA objects for multi-turn/image-question responses. |
Source code in urbanworm/UrbanDataSet.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
__checkModel(model)
¶
Check if the model is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
The model name. |
required |
Source code in urbanworm/UrbanDataSet.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
__init__(image=None, images=None, units=None, format=None, mapillary_key=None, random_sample=None)
¶
Add data or api key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
str
|
The path to the image. |
None
|
images
|
list
|
The list of image paths. |
None
|
units
|
str or GeoDataFrame
|
The path to the shapefile or geojson file, or GeoDataFrame. |
None
|
format
|
Response
|
The response format. |
None
|
mapillary_key
|
str
|
The Mapillary API key. |
None
|
random_sample
|
int
|
The number of random samples. |
None
|
Source code in urbanworm/UrbanDataSet.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
__summarize_geo_df(max_rows=2)
¶
Summarize key characteristics of self.geo_df for LLM context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_rows
|
int
|
Number of sample rows to return. |
2
|
Returns:
Type | Description |
---|---|
tuple[str, list[dict]]
|
tuple[str, list]: (summary string, example row list) |
Source code in urbanworm/UrbanDataSet.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
|
bbox2Buildings(bbox, source='osm', epsg=None, min_area=0, max_area=None, random_sample=None)
¶
Extract buildings from OpenStreetMap using the bbox.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox
|
list or tuple
|
The bounding box. |
required |
source
|
str
|
The source of the buildings. ['osm', 'bing'] |
'osm'
|
epsg
|
int
|
EPSG code for coordinate transformation. Required if source='bing' and (min_area > 0 or max_area) is specified. |
None
|
min_area
|
float or int
|
The minimum area. |
0
|
max_area
|
float or int
|
The maximum area. |
None
|
random_sample
|
int
|
The number of random samples. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The number of buildings found in the bounding box |
Source code in urbanworm/UrbanDataSet.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
chat(model='gemma3:12b', system=None, prompt=None, img=None, temp=None, top_k=None, top_p=None)
¶
Chat with the LLM model using a system message, prompt, and optional image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model name. Defaults to "gemma3:12b". ['granite3.2-vision', 'llama3.2-vision', 'gemma3', 'gemma3:1b', 'gemma3:12b', 'minicpm-v', 'mistral-small3.1'] |
'gemma3:12b'
|
system
|
str
|
The system-level instruction for the model. |
None
|
prompt
|
str
|
The user message or question. |
None
|
img
|
str
|
Path to a single image to be sent to the model. |
None
|
temp
|
float
|
Sampling temperature for generation (higher = more random). |
None
|
top_k
|
float
|
Top-k sampling parameter. |
None
|
top_p
|
float
|
Top-p (nucleus) sampling parameter. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Response |
Response
|
Parsed response from the LLM, returned as a |
Source code in urbanworm/UrbanDataSet.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
dataAnalyst(prompt, system='You are a spatial data analyst.', model='gemma3')
¶
Conversational spatial data analysis using a language model, with context-aware initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
User query related to spatial analysis. |
required |
system
|
str
|
Base system prompt for the assistant. |
'You are a spatial data analyst.'
|
model
|
str
|
LLM model name to use. |
'gemma3'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in urbanworm/UrbanDataSet.py
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
|
export(out_type, file_name)
¶
Exports the result to a specified spatial data format.
This method saves the spatial data stored in self.geo_df
to a file in the specified format.
If the GeoDataFrame is not yet initialized, it will attempt to convert the results first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_type
|
str
|
The output file format. Options include: 'geojson': Exports the data as a GeoJSON file; 'shapefile' : Exports the data as an ESRI Shapefile. 'geopackage': Exports the data as a GeoPackage (GPKG). |
required |
file_name
|
str
|
The path and file name where the data will be saved.
For shapefiles, provide a |
required |
Returns: None
Source code in urbanworm/UrbanDataSet.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
|
loopImgChat(model='gemma3:12b', system=None, prompt=None, temp=0.0, top_k=1.0, top_p=0.8, saveImg=False, output_df=False, disableProgressBar=False)
¶
Chat with MLLM model for each image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model name. Defaults to "gemma3:12b". ['granite3.2-vision', 'llama3.2-vision', 'gemma3', 'gemma3:1b', 'gemma3:12b', 'minicpm-v', 'mistral-small3.1'] |
'gemma3:12b'
|
system
|
(str, optinal)
|
The system message. |
None
|
prompt
|
str
|
The prompt message. |
None
|
temp
|
float
|
The temperature value. |
0.0
|
top_k
|
float
|
The top_k value. |
1.0
|
top_p
|
float
|
The top_p value. |
0.8
|
saveImg
|
bool
|
The saveImg for saving each image in base64 format in the output. |
False
|
output_df
|
bool
|
The output_df for saving the result in a pandas DataFrame. Defaults to False. |
False
|
disableProgressBar
|
bool
|
The progress bar for showing the progress of data analysis over the units |
False
|
Returns:
Type | Description |
---|---|
dict
|
list A list of dictionaries. Each dict includes questions/messages, responses/answers, and image base64 (if required) |
Source code in urbanworm/UrbanDataSet.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
loopUnitChat(model='gemma3:12b', system=None, prompt=None, temp=0.0, top_k=1.0, top_p=0.8, type='top', epsg=None, multi=False, sv_fov=80, sv_pitch=10, sv_size=(300, 400), year=None, season=None, time_of_day=None, saveImg=True, output_gdf=False, disableProgressBar=False)
¶
Chat with the MLLM model for each spatial unit in the shapefile.
This function loops through all units (e.g., buildings or parcels) in self.units
,
generates top and/or street view images, and prompts a language model
with custom messages. It stores results in self.results
.
When finished, your self.results object looks like this:
{
'from_loopUnitChat': {
'lon': [...],
'lat': [...],
'top_view': [[QnA, QnA, ...], ...],
'street_view': [[QnA, QnA, ...], ...],
},
'base64_imgs': {
'top_view_base64': [...],
'street_view_base64': [...],
}
}
Example prompt:
prompt = {
"top": "
Is there any damage on the roof?
",
"street": "
Is the wall missing or damaged?
Is the yard maintained well?
"
}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model name. Defaults to "gemma3:12b". ['granite3.2-vision', 'llama3.2-vision', 'gemma3', 'gemma3:1b', 'gemma3:12b', 'gemma3:27b', 'minicpm-v', 'mistral-small3.1] |
'gemma3:12b'
|
system
|
str
|
System message to guide the LLM behavior. |
None
|
prompt
|
dict
|
Dictionary containing the prompts for 'top' and/or 'street' views. |
None
|
temp
|
float
|
Temperature for generation randomness. Defaults to 0.0. |
0.0
|
top_k
|
float
|
Top-k sampling parameter. Defaults to 1.0. |
1.0
|
top_p
|
float
|
Top-p sampling parameter. Defaults to 0.8. |
0.8
|
type
|
str
|
Which image type(s) to use: "top", "street", or "both". Defaults to "top". |
'top'
|
epsg
|
int
|
EPSG code for coordinate transformation. Required if type includes "street". |
None
|
multi
|
bool
|
Whether to return multiple SVIs per unit. Defaults to False. |
False
|
sv_fov
|
int
|
Field of view for street view. Defaults to 80. |
80
|
sv_pitch
|
int
|
Pitch angle for street view. Defaults to 10. |
10
|
sv_size
|
(list, tuple)
|
Size (height, width) for street view images. Defaults to (300, 400). |
(300, 400)
|
year
|
list or tuple
|
The year ranges (e.g., (2018,2023)). |
None
|
season
|
str
|
'spring', 'summer', 'fall', 'winter'. |
None
|
time_of_day
|
str
|
'day' or 'night'. |
None
|
saveImg
|
bool
|
Whether to save images (as base64 strings) in output. Defaults to True. |
True
|
output_gdf
|
bool
|
Whether to return results as a GeoDataFrame. Defaults to False. |
False
|
disableProgressBar
|
bool
|
Whether to show progress bar. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing prompts, responses, and (optionally) image data for each unit. |
Source code in urbanworm/UrbanDataSet.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
oneImgChat(model='gemma3:12b', system=None, prompt=None, temp=0.0, top_k=1.0, top_p=0.8, saveImg=True)
¶
Chat with MLLM model with one image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model name. Defaults to "gemma3:12b". ['granite3.2-vision', 'llama3.2-vision', 'gemma3', 'gemma3:1b', 'gemma3:12b', 'minicpm-v', 'mistral-small3.1'] |
'gemma3:12b'
|
system
|
optinal
|
The system message. |
None
|
prompt
|
str
|
The prompt message. |
None
|
img
|
str
|
The image path. |
required |
temp
|
float
|
The temperature value. |
0.0
|
top_k
|
float
|
The top_k value. |
1.0
|
top_p
|
float
|
The top_p value. |
0.8
|
saveImg
|
bool
|
The saveImg for save each image in base64 format in the output. |
True
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary includes questions/messages, responses/answers, and image base64 (if required) |
Source code in urbanworm/UrbanDataSet.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
plotBase64(img)
¶
plot a single base64 image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img
|
str
|
image base64 string |
required |
Source code in urbanworm/UrbanDataSet.py
702 703 704 705 706 707 708 709 |
|
plot_gdf(figsize=(12, 10), summary_func=None, show_table=True)
¶
Visualize all Q&A pairs from geo_df as separate maps with optional answer tables.
- Automatically adjusts color scheme based on answer data type:
- Numeric answers → gradient cmap (viridis)
- Categorical answers (string/bool) → color-coded groups (case-insensitive)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
figsize
|
tuple
|
Figure size. |
(12, 10)
|
summary_func
|
callable
|
Function to reduce list-type fields (e.g., lambda x: x[0]). |
None
|
show_table
|
bool
|
Whether to include an answer table. |
True
|
Source code in urbanworm/UrbanDataSet.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
|
preload_model(model_name)
¶
Ensures that the required Ollama model is available. If not, it automatically pulls the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
model name |
required |
Source code in urbanworm/UrbanDataSet.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
to_df(output=True)
¶
Convert the output from an MLLM reponse (from .loopImgChat) into a DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
bool
|
Whether to return a DataFrame. Defaults to True. |
True
|
Returns:
pd.DataFrame: A DataFrame containing responses and associated metadata.
str: An error message if .loopImgChat()
has not been run or if the format is unsupported.
Source code in urbanworm/UrbanDataSet.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
to_gdf(output=True)
¶
Convert the output from an MLLM response (from .loopUnitChat) into a GeoDataFrame.
This method extracts coordinates, questions, responses, and base64-encoded input images
from the stored self.results
object, and formats them into a structured GeoDataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
bool
|
Whether to return a GeoDataFrame. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
GeoDataFrame | str
|
gpd.GeoDataFrame: A GeoDataFrame containing spatial responses and associated metadata. |
|
str |
GeoDataFrame | str
|
An error message if |
Source code in urbanworm/UrbanDataSet.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|