Skip to content

Commit 3c91575

Browse files
committed
added geojson snippet
1 parent d4f75f5 commit 3c91575

File tree

1 file changed

+61
-51
lines changed

1 file changed

+61
-51
lines changed

geo/geojson.ipynb

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,26 @@
1818
"\n",
1919
"\n",
2020
"> __Note:__ The data is from the _Open Data Platform Swiss Public Transport_, https://opentransportdata.swiss/en/\n",
21-
"\n"
21+
"\n",
22+
"\n",
23+
"### Required libraries\n",
24+
"- pandas, http://pandas.pydata.org/\n",
25+
"- geojson, https://pypi.python.org/pypi/geojson/\n"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"### Load the data\n",
33+
"First let's load the data with pandas. The data frame contains the stations from the public transportations from Switzerland and some from adjoining countries. We have the columns:\n",
34+
"- StationID\n",
35+
"- Longitude\n",
36+
"- Latitude\n",
37+
"- Height\n",
38+
"- Remark\n",
39+
"\n",
40+
"Longitude and Latitude should be _WGS 84_ coordinates."
2241
]
2342
},
2443
{
@@ -103,13 +122,19 @@
103122
}
104123
],
105124
"source": [
106-
"\n",
107125
"import pandas as pd\n",
108126
"\n",
109127
"df = pd.read_csv('data/bfkoordgeo_utf8.csv')\n",
110128
"df.head()"
111129
]
112130
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"Now we do some data cleaning and remove all rows where _Longitude_ and _Latitude_ are _'null'_."
136+
]
137+
},
113138
{
114139
"cell_type": "code",
115140
"execution_count": 2,
@@ -119,19 +144,24 @@
119144
},
120145
"outputs": [],
121146
"source": [
122-
"\n",
123147
"df = df[df['Longitude'].notnull()]\n",
124148
"df = df[df['Latitude'].notnull()]\n",
125149
"\n",
126-
"#df[df.isnull().any(axis=1)]\n",
127-
"\n",
128-
"\n",
129-
"import geojson as geojson"
150+
"# will display all rows that have null values\n",
151+
"#df[df.isnull().any(axis=1)]"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"### Convert pandas data frame to GEOJSON\n",
159+
"Next we convert the panda data frame to geosjon _FeatureCollection/Feature/Point_. "
130160
]
131161
},
132162
{
133163
"cell_type": "code",
134-
"execution_count": 14,
164+
"execution_count": 3,
135165
"metadata": {
136166
"collapsed": false
137167
},
@@ -145,70 +175,50 @@
145175
}
146176
],
147177
"source": [
148-
"\n",
178+
"import geojson as geojson\n",
149179
"\n",
150180
"values = zip(df['Longitude'], df['Latitude'], df['Remark'])\n",
151181
"points = [geojson.Feature(geometry=geojson.Point((v[0], v[1])), properties={'name': v[2]}) for v in values]\n",
152182
"\n",
183+
"geo_collection = geojson.FeatureCollection(points)\n",
184+
"\n",
153185
"print(points[0])"
154186
]
155187
},
156188
{
157-
"cell_type": "code",
158-
"execution_count": 18,
159-
"metadata": {
160-
"collapsed": false
161-
},
162-
"outputs": [],
189+
"cell_type": "markdown",
190+
"metadata": {},
163191
"source": [
164-
"\n",
165-
"\n",
166-
"geo_collection = geojson.FeatureCollection(points)\n",
167-
"\n"
192+
"### Save the geojson (FeatureCollection) to a file\n",
193+
"Finally we dump the geojson to a file."
168194
]
169195
},
170196
{
171197
"cell_type": "code",
172-
"execution_count": 19,
198+
"execution_count": 4,
173199
"metadata": {
174200
"collapsed": false
175201
},
176-
"outputs": [],
202+
"outputs": [
203+
{
204+
"data": {
205+
"text/plain": [
206+
"\"\\nwith open('stations.geojson', 'w') as file:\\n file.write(dump)\\n\""
207+
]
208+
},
209+
"execution_count": 4,
210+
"metadata": {},
211+
"output_type": "execute_result"
212+
}
213+
],
177214
"source": [
178215
"dump = geojson.dumps(geo_collection, sort_keys=True)\n",
179-
"\n"
180-
]
181-
},
182-
{
183-
"cell_type": "code",
184-
"execution_count": 20,
185-
"metadata": {
186-
"collapsed": false
187-
},
188-
"outputs": [],
189-
"source": [
190216
"\n",
217+
"'''\n",
191218
"with open('stations.geojson', 'w') as file:\n",
192-
" file.write(dump)\n"
219+
" file.write(dump)\n",
220+
"'''"
193221
]
194-
},
195-
{
196-
"cell_type": "code",
197-
"execution_count": null,
198-
"metadata": {
199-
"collapsed": false
200-
},
201-
"outputs": [],
202-
"source": []
203-
},
204-
{
205-
"cell_type": "code",
206-
"execution_count": null,
207-
"metadata": {
208-
"collapsed": true
209-
},
210-
"outputs": [],
211-
"source": []
212222
}
213223
],
214224
"metadata": {

0 commit comments

Comments
 (0)