Skip to content

Commit 207e73b

Browse files
committed
Initial commit
0 parents  commit 207e73b

6 files changed

+1380
-0
lines changed

Create_Masks.ipynb

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Here, we want to create mask-files\n",
10+
"# from the geojson-files"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"# GeoJSON --> .png"
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"Plot and save the given coordinates in GeoJSON files."
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"import numpy as np\n",
34+
"import matplotlib.pyplot as plt\n",
35+
"import os\n",
36+
"import json\n",
37+
"import geoio"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"ResimPATH = 'TRAINING TIF-FILE PATH/Train/AOI_2_Vegas_Train/RGB-PanSharpen' \n",
47+
"# All the pictures from the given path\n",
48+
"ResimAdlari = os.listdir(ResimPATH)\n",
49+
"\n",
50+
"GeoJSONPATH = 'TRAINING GEOJSON-FILE PATH/Train/AOI_2_Vegas_Train/geojson/buildings'\n",
51+
"# All the geojson-files from the given path\n",
52+
"GeoAdlari = os.listdir(GeoJSONPATH)"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"metadata": {
59+
"scrolled": false
60+
},
61+
"outputs": [],
62+
"source": [
63+
"# DosyaNumarasi takes the number from 0 to how big your training dataset\n",
64+
"for DosyaNumarasi in range(len(GeoAdlari)):\n",
65+
"\n",
66+
" # data holds the info for the each GeoJSON-file\n",
67+
" with open(GeoJSONPATH+'/'+GeoAdlari[DosyaNumarasi]) as f:\n",
68+
" data = json.load(f)\n",
69+
" \n",
70+
" # RGBTIFResmi holds the info for each TIF-file\n",
71+
" RGBTIFResmi = geoio.GeoImage(ResimPATH+'/'+ResimAdlari[DosyaNumarasi])\n",
72+
"\n",
73+
" cokgenler = [] # Hold the coordinates for each building in the picture.\n",
74+
" # (Outside for loop is for each picture, and here, cokgenler\n",
75+
" # will hold the coordinates for each building in one picture.)\n",
76+
" types = [] # Holds the type of the buldings (MultiPolygon - Partial Building - Point)\n",
77+
" # We are not interested in the points.\n",
78+
" \n",
79+
" # Create the pane size of 650x650 to put the figures from geojson-file. Otherwise,\n",
80+
" # the buildings may be flipped or they may saved one by one \n",
81+
" arkaPlan = np.zeros([650,650])\n",
82+
" plt.imshow(arkaPlan)\n",
83+
"\n",
84+
" try:\n",
85+
" # We do not know how many buildings the picture includes.\n",
86+
" # So, we just give very big number to make sure that we utilized\n",
87+
" # all the buildings in one picture.\n",
88+
" # In short, bina keeps what order the building is.\n",
89+
" for bina in range(2000):\n",
90+
" tip = str(data['features'][bina]['geometry']['type'])\n",
91+
" types.append(tip) # Append all the type of the buildings\n",
92+
"\n",
93+
" # If type is point, do not do anything\n",
94+
" if tip == ('Point'):\n",
95+
" pass\n",
96+
" \n",
97+
" # If type is MultiPolygon, cokgenler will hold the coordinates\n",
98+
" elif tip == ('MultiPolygon'):\n",
99+
" kucukBinalar = (data['features'][bina]['geometry']['coordinates'])\n",
100+
" for b in range(len(kucukBinalar)): \n",
101+
" cokgenler.append(kucukBinalar[b])\n",
102+
" \n",
103+
" # For the rest of the types, cokgenler will hold the coordinates again\n",
104+
" else:\n",
105+
" cokgenler.append(data['features'][bina]['geometry']['coordinates'])\n",
106+
"\n",
107+
" except IndexError:\n",
108+
" # If we utilized all the buildings in the given picture,\n",
109+
" # lest create mask for each one.\n",
110+
" \n",
111+
" # cokgenBina holds the each building's coordinates\n",
112+
" for cokgenBina in cokgenler: \n",
113+
"\n",
114+
" # binaNoktalari holds the individual edge coordinates for each building.\n",
115+
" for binaNoktalari in cokgenBina:\n",
116+
" \n",
117+
" # To hold the edge coordinates (in pixel form)\n",
118+
" doldurX = []\n",
119+
" doldurY = []\n",
120+
"\n",
121+
" # noktas holds x and y for each edge coordinate\n",
122+
" for noktas in binaNoktalari:\n",
123+
" \n",
124+
" # Convert Latitude&Longitude to the pixels\n",
125+
" xPixel, yPixel = RGBTIFResmi.proj_to_raster(noktas[0], noktas[1])\n",
126+
" \n",
127+
" # The pixels may be 650 which defaces the masks.\n",
128+
" xPixel = 649 if xPixel > 649 else xPixel\n",
129+
" yPixel = 649 if yPixel > 649 else yPixel\n",
130+
" \n",
131+
" # Keep x and y in pixel form\n",
132+
" doldurX.append(xPixel)\n",
133+
" doldurY.append(yPixel)\n",
134+
" \n",
135+
" # To paint between given pixel values\n",
136+
" plt.fill_between(doldurX, doldurY, facecolor='red')\n",
137+
" \n",
138+
" # To remove white area around matplotlib figure\n",
139+
" fig = plt.figure(1)\n",
140+
" extent = plt.gca().get_window_extent().transformed(fig.dpi_scale_trans.inverted())\n",
141+
"\n",
142+
" # Adjust the DPI for 650x650\n",
143+
" # and save the figure\n",
144+
" # While saving, you should put them in order; 0 to ...\n",
145+
" fig.savefig('Where to save the figure '+str(DosyaNumarasi)+'.png', bbox_inches=extent, dpi=215.24)\n",
146+
" \n",
147+
" # Close the figure after an image is done.\n",
148+
" plt.close()"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": []
157+
}
158+
],
159+
"metadata": {
160+
"kernelspec": {
161+
"display_name": "Python 3",
162+
"language": "python",
163+
"name": "python3"
164+
},
165+
"language_info": {
166+
"codemirror_mode": {
167+
"name": "ipython",
168+
"version": 3
169+
},
170+
"file_extension": ".py",
171+
"mimetype": "text/x-python",
172+
"name": "python",
173+
"nbconvert_exporter": "python",
174+
"pygments_lexer": "ipython3",
175+
"version": "3.6.7"
176+
}
177+
},
178+
"nbformat": 4,
179+
"nbformat_minor": 2
180+
}

0 commit comments

Comments
 (0)