Skip to content

Instantly share code, notes, and snippets.

@hkayabilisim
Created February 20, 2023 06:43
Show Gist options
  • Save hkayabilisim/81823a4c5856f0b90490b9828ebab995 to your computer and use it in GitHub Desktop.
Save hkayabilisim/81823a4c5856f0b90490b9828ebab995 to your computer and use it in GitHub Desktop.
import geopandas as gp
import pandas as pd
from owslib.wms import WebMapService
import owslib.util as util
import matplotlib.pyplot as plt
from shapely.geometry import box
import os
wms = WebMapService('https://maps.cscrs.itu.edu.tr/geoserver/ows?service=WMS',
username='****',password='****', version='1.3.0')
df = pd.read_csv('hasarli_geocoded_10_02_2023.csv')
buildings = gp.GeoDataFrame(
df, geometry=gp.points_from_xy(df.lon, df.lat), crs='EPSG:4326')
names = list(wms.contents)
bboxes = []
srids = []
layer_types = []
for name in names:
xmin,ymin,xmax,ymax, srid = wms[name].boundingBox
if wms[name].keywords[0] == 'features':
layer_types.append('vector')
else:
layer_types.append('raster')
bboxes.append(box(xmin,ymin,xmax,ymax))
srids.append(srid)
layers = gp.GeoDataFrame({'name': names, 'geometry': bboxes, 'srid': srids,
'layer_type': layer_types})
layers = layers.set_crs('EPSG:4326')
raster_layers = layers[layers['layer_type'] == 'raster']
vector_layers = layers[layers['layer_type'] == 'vector']
building_layers = buildings.sjoin(raster_layers, how='left', predicate='within')
for label in pd.unique(buildings['durum']):
try:
os.mkdir(f'images/{label}')
except:
print(f'error in creating {label} folder')
unique_frames = []
max_len = len(building_layers)
for i, row in building_layers.iterrows():
label=row['durum']
uid = row['uid']
lon = row['lon']
lat = row['lat']
layer_name = row['name']
frame_hash = f'{lon}_{lat}_{layer_name}'
err = ''
if row['geometry'].is_empty:
err = 'ERR:building location not specified'
if not isinstance(layer_name, str):
err = 'ERR:no raster layer contains the building'
if frame_hash in unique_frames:
err = f'ERR:previously generated {frame_hash}'
else:
unique_frames.append(frame_hash)
if err != '':
print(f"{i:5d}/{max_len} {label:20s} {uid:20s} {err}")
continue
if "oncesi" in layer_name.lower():
label = 'oncesi'
fname = f'{uid}_{lon}_{lat}_{layer_name}.jpg'
fullpath = f'images/{label}/{fname}'
building_rect = row['geometry'].buffer(0.001,cap_style=3)
img = wms.getmap(
layers=[layer_name],
srs='CRS:84',
bbox=building_rect.bounds,
size=(224, 224),
format='image/jpeg',
transparent=True
)
out = open(fullpath, 'wb')
out.write(img.read())
out.close()
print(f"{i:5d}/{max_len} {label:20s} {uid:20s} {frame_hash:40s}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment