|
18 | 18 | "\n",
|
19 | 19 | "\n",
|
20 | 20 | "> __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." |
22 | 41 | ]
|
23 | 42 | },
|
24 | 43 | {
|
|
103 | 122 | }
|
104 | 123 | ],
|
105 | 124 | "source": [
|
106 |
| - "\n", |
107 | 125 | "import pandas as pd\n",
|
108 | 126 | "\n",
|
109 | 127 | "df = pd.read_csv('data/bfkoordgeo_utf8.csv')\n",
|
110 | 128 | "df.head()"
|
111 | 129 | ]
|
112 | 130 | },
|
| 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 | + }, |
113 | 138 | {
|
114 | 139 | "cell_type": "code",
|
115 | 140 | "execution_count": 2,
|
|
119 | 144 | },
|
120 | 145 | "outputs": [],
|
121 | 146 | "source": [
|
122 |
| - "\n", |
123 | 147 | "df = df[df['Longitude'].notnull()]\n",
|
124 | 148 | "df = df[df['Latitude'].notnull()]\n",
|
125 | 149 | "\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_. " |
130 | 160 | ]
|
131 | 161 | },
|
132 | 162 | {
|
133 | 163 | "cell_type": "code",
|
134 |
| - "execution_count": 14, |
| 164 | + "execution_count": 3, |
135 | 165 | "metadata": {
|
136 | 166 | "collapsed": false
|
137 | 167 | },
|
|
145 | 175 | }
|
146 | 176 | ],
|
147 | 177 | "source": [
|
148 |
| - "\n", |
| 178 | + "import geojson as geojson\n", |
149 | 179 | "\n",
|
150 | 180 | "values = zip(df['Longitude'], df['Latitude'], df['Remark'])\n",
|
151 | 181 | "points = [geojson.Feature(geometry=geojson.Point((v[0], v[1])), properties={'name': v[2]}) for v in values]\n",
|
152 | 182 | "\n",
|
| 183 | + "geo_collection = geojson.FeatureCollection(points)\n", |
| 184 | + "\n", |
153 | 185 | "print(points[0])"
|
154 | 186 | ]
|
155 | 187 | },
|
156 | 188 | {
|
157 |
| - "cell_type": "code", |
158 |
| - "execution_count": 18, |
159 |
| - "metadata": { |
160 |
| - "collapsed": false |
161 |
| - }, |
162 |
| - "outputs": [], |
| 189 | + "cell_type": "markdown", |
| 190 | + "metadata": {}, |
163 | 191 | "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." |
168 | 194 | ]
|
169 | 195 | },
|
170 | 196 | {
|
171 | 197 | "cell_type": "code",
|
172 |
| - "execution_count": 19, |
| 198 | + "execution_count": 4, |
173 | 199 | "metadata": {
|
174 | 200 | "collapsed": false
|
175 | 201 | },
|
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 | + ], |
177 | 214 | "source": [
|
178 | 215 | "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": [ |
190 | 216 | "\n",
|
| 217 | + "'''\n", |
191 | 218 | "with open('stations.geojson', 'w') as file:\n",
|
192 |
| - " file.write(dump)\n" |
| 219 | + " file.write(dump)\n", |
| 220 | + "'''" |
193 | 221 | ]
|
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": [] |
212 | 222 | }
|
213 | 223 | ],
|
214 | 224 | "metadata": {
|
|
0 commit comments