<jupyter_start><jupyter_text>Python tifffile package: https://github.com/cgohlke/tifffile/<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import tifffile as tf
import geopandas as gpd
from osgeo import gdal<jupyter_output><empty_output><jupyter_text>Using tifffile<jupyter_code>lu = plt.imread('ConwyLU.tif')
slope = plt.imread('ConwySlope.tif')
status = plt.imread('ConwyStatus.tif')
print(lu.shape, slope.shape, status.shape)
fig, axs = plt.subplots(1, 3, figsize=(20, 6))
axs[0].imshow(lu, cmap='gray')
axs[1].imshow(slope)
axs[2].imshow(status)

plt.show()<jupyter_output><empty_output><jupyter_text>Using GDAL<jupyter_code>lu2 = gdal.Open(r'ConwyLU.tif')
slope2 = gdal.Open(r'ConwySlope.tif')
status2 = gdal.Open(r'ConwyStatus.tif')
files = [lu2, slope2, status2]
print('ConwyLU: # of bands: {}, X: {}, Y: {}'.format(lu2.RasterCount, lu2.RasterXSize, lu2.RasterYSize))
print('ConwySlope: # of bands: {}, X: {}, Y: {}'.format(slope2.RasterCount, slope2.RasterXSize, slope2.RasterYSize))
print('ConwyStatus: # of bands: {}, X: {}, Y: {}'.format(status2.RasterCount, status2.RasterXSize, status2.RasterYSize))
bands = [lu2.GetRasterBand(1), slope2.GetRasterBand(1), status2.GetRasterBand(1)]

print("Statistics: min, max, mean, standard deviation")
print('ConwyLU: {}'.format(bands[0].GetStatistics(True, True)))
print('ConwySlope: {}'.format(bands[1].GetStatistics(True, True)))
print('ConwyStatus: {}'.format(bands[2].GetStatistics(True, True)))

arrs = [band.ReadAsArray() for band in bands]

fig, axs = plt.subplots(1, 3, figsize=(24, 8))
axs[0].imshow(arrs[0], cmap='GnBu')
axs[0].set_title('Conwy LU')
axs[1].imshow(arrs[1], cmap='GnBu')
axs[1].set_title('Conwy Slope')
axs[2].imshow(arrs[2], cmap='GnBu')
axs[2].set_title('Conwy Status')

plt.show()<jupyter_output>Statistics: min, max, mean, standard deviation
ConwyLU: [1.0, 3.0, 2.1909979969365, 0.89966755095785]
ConwySlope: [0.0, 43.0, 8.8781044435495, 6.5999558465119]
ConwyStatus: [1.0, 4.0, 3.4168818448936, 0.87562008061435]<jupyter_text>Using rasteriohttps://rasterio.readthedocs.io/en/latest/<jupyter_code>import rasterio
import rasterio.features
import rasterio.warp
dataset = rasterio.open('ConwyLU.tif')
mask = dataset.dataset_mask()
for geom, val in rasterio.features.shapes(
            mask, transform=dataset.transform):
    print(geom)<jupyter_output>{'type': 'Polygon', 'coordinates': [[(277850.00000000093, 370000.0), (277850.00000000093, 369800.0), (278050.00000000093, 369800.0), (278050.00000000093, 370000.0), (277850.00000000093, 370000.0)]]}
{'type': 'Polygon', 'coordinates': [[(263850.00000000093, 382000.0), (263850.00000000093, 358000.0), (264250.00000000093, 358000.0), (264250.00000000093, 358200.0), (264850.00000000093, 358200.0), (264850.00000000093, 358400.0), (265650.00000000093, 358400.0), (265650.00000000093, 358600.0), (266050.00000000093, 358600.0), (266050.00000000093, 358400.0), (267050.00000000093, 358400.0), (267050.00000000093, 358600.0), (267250.00000000093, 358600.0), (267250.00000000093, 359000.0), (267450.00000000093, 359000.0), (267450.00000000093, 359600.0), (267250.00000000093, 359600.0), (267250.00000000093, 360000.0), (267450.00000000093, 360000.0), (267450.00000000093, 360600.0), (267650.00000000093, 360600.0), (267650.00000000093, 360800.0), (267850.00000000093, 360800.0), (267850.00000000093, 361200.[...]<jupyter_text>Using geopandas<jupyter_code>shp = gpd.read_file('Conwy.shp')
shp
shp.describe()
print(shp['LU'].value_counts(dropna=False))
print(shp['Status'].value_counts(dropna=False))
slopes = shp['Slope'].value_counts()
slopes.sort_index(inplace=True)

fig = plt.figure(figsize=(15, 8))
num_ticks = len(slopes)
plt.locator_params(axis="x", nbins=num_ticks)
# plt.plot(slopes)
plt.bar(slopes.index, slopes.values, alpha=0.5)
    
plt.xlabel("Slope")
plt.ylabel("Frequency")

plt.show()
slopes
shp.crs
# fig, axs = plt.subplots(1, 3, figsize=(20, 8))
f, ax = plt.subplots(1, figsize=(8, 8))
ax = shp.plot(ax=ax, column='Slope', legend=True, legend_kwds={'label': "Slope"})
plt.title("BY SLOPE")
plt.show()
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(30, 10))
shp.plot(ax=ax1, column='LU', cmap='GnBu', legend=True, legend_kwds={'label': "LU"})
ax1.set_title('Conwy LU')
shp.plot(ax=ax2, column='Status', cmap='YlOrBr', legend=True, legend_kwds={'label': "Status"})
ax2.set_title('Conwy Status')
shp.plot(ax=ax3, column='Slope', cmap='YlGn', legend=True, legend_kwds={'label': "Slope"})
ax3.set_title('Conwy Slope')

plt.show()
fig, ax = plt.subplots(1, figsize=(10, 10))
shp.boundary.plot(ax=ax)

plt.show()
shx = gpd.read_file('Conwy.shx')
shx
f, ax = plt.subplots(1, figsize=(8, 8))
ax = shx.plot(ax=ax, column='Slope', legend=True, legend_kwds={'label': "Slope"})
plt.title("BY SLOPE")
plt.show()
dbf = gpd.read_file('Conwy.dbf')
dbf
f, ax = plt.subplots(1, figsize=(8, 8))
ax = dbf.plot(ax=ax, column='Slope', legend=True, legend_kwds={'label': "Slope"})
plt.title("BY SLOPE")
plt.show()
# pip install pyshp
import shapefile
import osr
from io import StringIO
dbf_new = open("Conwy.dbf", "rb")
shp_new = open("Conwy.shp", "rb")
shx_new = open("Conwy.shx", "rb")

# prj_new = open("Conwy.prj", "rb")
# prj_new = shapefile.Reader("Conwy.prj")
prj_new = StringIO("Conwy.prj")
r = shapefile.Reader(shp=shp_new, shx=shx_new, dbf=dbf_new)
r.numRecords
prj_tmp = 'PROJCS["OSGB_1936_British_National_Grid",GEOGCS["GCS_OSGB 1936",DATUM["D_OSGB_1936",SPHEROID["Airy_1830",6377563.396,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",49],PARAMETER["central_meridian",-2],PARAMETER["scale_factor",0.9996012717],PARAMETER["false_easting",400000],PARAMETER["false_northing",-100000],UNIT["Meter",1]]'
prj4 = osr.SpatialReference(prj_tmp).ExportToProj4()
prj4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19245283018867926, 'fraction_numerical': 0.1653516295025729, 'mean_word_length': 7.715994020926757, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 29, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '177862', 'license': 'MIT', 'n_tokens_mistral': 3127, 'n_tokens_neox': 2492, 'n_words': 378, 'path': 'Ngoc/Week 2/Week 2.ipynb', 'repo_name': 'sashanksilwal/CRSLR', 'size': '5830'}