{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Read data from a JSON file\n", "\n", "This example illustrates how to read data from a remote JSON file using pandas and the process of preparing the data for spatial operations." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import requests\n", "\n", "# Download the JSON file\n", "remote_file_path = 'https://libs.cartocdn.com/cartoframes/samples/arbres_remarquables_paris.json'\n", "data_json = requests.get(remote_file_path).json()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datasetidrecordidrecord_timestampfields.geom_x_yfields.libellefrancaisfields.objectidfields.idemplacementfields.arrondissementfields.circonferenceencmfields.hauteurenm...fields.stadedeveloppementfields.remarquablefields.idbasefields.genrefields.complementadressefields.typeemplacementfields.dateplantationgeometry.typegeometry.coordinatesfields.varieteoucultivar
0arbresremarquablesparisa236960f295288698ca57fdccfd9825547ffb4d02019-10-04T10:00:41.063000+00:00[48.8630096006, 2.2417737512]Pin49884000901001BOIS DE BOULOGNE241.030.0...M12002349.0Pinus16-08Arbre1882-01-01T00:09:21+00:00Point[2.2417737512, 48.8630096006]NaN
1arbresremarquablesparisd7635937c3636051c99963676d3a97dd4d9dd8c92019-10-04T10:00:41.063000+00:00[48.8731110898, 2.24886478886]Hêtre52761000701004BOIS DE BOULOGNE231.016.0...M12002354.0Fagus16-17Arbre1868-01-01T00:09:21+00:00Point[2.24886478886, 48.8731110898]''Pendula''
2arbresremarquablesparis460e8eccd3ebc816f16317702223bf406e7d75e62019-10-04T10:00:41.063000+00:00[48.8448875264, 2.35366916319]Hêtre6245600000075PARIS 5E ARRDT85.03.0...M1132669.0FagusNaNArbre1905-01-01T00:09:21+00:00Point[2.35366916319, 48.8448875264]''Tortuosa''
3arbresremarquablesparisb60b80e0f8327afba6d0c513b0b6b85639e516b62019-10-04T10:00:41.063000+00:00[48.8787029872, 2.30647417371]Erable6880100NO0144PARIS 8E ARRDT450.028.0...M1305507.0Acer08-01Arbre1700-01-01T00:09:21+00:00Point[2.30647417371, 48.8787029872]NaN
4arbresremarquablesparis47e163d6f0536bc9e26ba2993447c6f50b14a6f62019-10-04T10:00:41.063000+00:00[48.8671184424, 2.25294211329]Platane82341001201001BOIS DE BOULOGNE534.045.0...M12002389.0Platanus16-10Arbre1872-01-01T00:09:21+00:00Point[2.25294211329, 48.8671184424]NaN
\n", "

5 rows × 24 columns

\n", "
" ], "text/plain": [ " datasetid recordid \\\n", "0 arbresremarquablesparis a236960f295288698ca57fdccfd9825547ffb4d0 \n", "1 arbresremarquablesparis d7635937c3636051c99963676d3a97dd4d9dd8c9 \n", "2 arbresremarquablesparis 460e8eccd3ebc816f16317702223bf406e7d75e6 \n", "3 arbresremarquablesparis b60b80e0f8327afba6d0c513b0b6b85639e516b6 \n", "4 arbresremarquablesparis 47e163d6f0536bc9e26ba2993447c6f50b14a6f6 \n", "\n", " record_timestamp fields.geom_x_y \\\n", "0 2019-10-04T10:00:41.063000+00:00 [48.8630096006, 2.2417737512] \n", "1 2019-10-04T10:00:41.063000+00:00 [48.8731110898, 2.24886478886] \n", "2 2019-10-04T10:00:41.063000+00:00 [48.8448875264, 2.35366916319] \n", "3 2019-10-04T10:00:41.063000+00:00 [48.8787029872, 2.30647417371] \n", "4 2019-10-04T10:00:41.063000+00:00 [48.8671184424, 2.25294211329] \n", "\n", " fields.libellefrancais fields.objectid fields.idemplacement \\\n", "0 Pin 49884 000901001 \n", "1 Hêtre 52761 000701004 \n", "2 Hêtre 62456 00000075 \n", "3 Erable 68801 00NO0144 \n", "4 Platane 82341 001201001 \n", "\n", " fields.arrondissement fields.circonferenceencm fields.hauteurenm ... \\\n", "0 BOIS DE BOULOGNE 241.0 30.0 ... \n", "1 BOIS DE BOULOGNE 231.0 16.0 ... \n", "2 PARIS 5E ARRDT 85.0 3.0 ... \n", "3 PARIS 8E ARRDT 450.0 28.0 ... \n", "4 BOIS DE BOULOGNE 534.0 45.0 ... \n", "\n", " fields.stadedeveloppement fields.remarquable fields.idbase fields.genre \\\n", "0 M 1 2002349.0 Pinus \n", "1 M 1 2002354.0 Fagus \n", "2 M 1 132669.0 Fagus \n", "3 M 1 305507.0 Acer \n", "4 M 1 2002389.0 Platanus \n", "\n", " fields.complementadresse fields.typeemplacement fields.dateplantation \\\n", "0 16-08 Arbre 1882-01-01T00:09:21+00:00 \n", "1 16-17 Arbre 1868-01-01T00:09:21+00:00 \n", "2 NaN Arbre 1905-01-01T00:09:21+00:00 \n", "3 08-01 Arbre 1700-01-01T00:09:21+00:00 \n", "4 16-10 Arbre 1872-01-01T00:09:21+00:00 \n", "\n", " geometry.type geometry.coordinates fields.varieteoucultivar \n", "0 Point [2.2417737512, 48.8630096006] NaN \n", "1 Point [2.24886478886, 48.8731110898] ''Pendula'' \n", "2 Point [2.35366916319, 48.8448875264] ''Tortuosa'' \n", "3 Point [2.30647417371, 48.8787029872] NaN \n", "4 Point [2.25294211329, 48.8671184424] NaN \n", "\n", "[5 rows x 24 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pandas import json_normalize\n", "\n", "# Normalize the data\n", "df = json_normalize(data_json['records'])\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datasetidrecordidrecord_timestampfields.geom_x_yfields.libellefrancaisfields.objectidfields.idemplacementfields.arrondissementfields.circonferenceencmfields.hauteurenm...fields.idbasefields.genrefields.complementadressefields.typeemplacementfields.dateplantationgeometry.typegeometry.coordinatesfields.varieteoucultivarlonlat
0arbresremarquablesparisa236960f295288698ca57fdccfd9825547ffb4d02019-10-04T10:00:41.063000+00:00[48.8630096006, 2.2417737512]Pin49884000901001BOIS DE BOULOGNE241.030.0...2002349.0Pinus16-08Arbre1882-01-01T00:09:21+00:00Point[2.2417737512, 48.8630096006]NaN2.24177448.863010
1arbresremarquablesparisd7635937c3636051c99963676d3a97dd4d9dd8c92019-10-04T10:00:41.063000+00:00[48.8731110898, 2.24886478886]Hêtre52761000701004BOIS DE BOULOGNE231.016.0...2002354.0Fagus16-17Arbre1868-01-01T00:09:21+00:00Point[2.24886478886, 48.8731110898]''Pendula''2.24886548.873111
2arbresremarquablesparis460e8eccd3ebc816f16317702223bf406e7d75e62019-10-04T10:00:41.063000+00:00[48.8448875264, 2.35366916319]Hêtre6245600000075PARIS 5E ARRDT85.03.0...132669.0FagusNaNArbre1905-01-01T00:09:21+00:00Point[2.35366916319, 48.8448875264]''Tortuosa''2.35366948.844888
3arbresremarquablesparisb60b80e0f8327afba6d0c513b0b6b85639e516b62019-10-04T10:00:41.063000+00:00[48.8787029872, 2.30647417371]Erable6880100NO0144PARIS 8E ARRDT450.028.0...305507.0Acer08-01Arbre1700-01-01T00:09:21+00:00Point[2.30647417371, 48.8787029872]NaN2.30647448.878703
4arbresremarquablesparis47e163d6f0536bc9e26ba2993447c6f50b14a6f62019-10-04T10:00:41.063000+00:00[48.8671184424, 2.25294211329]Platane82341001201001BOIS DE BOULOGNE534.045.0...2002389.0Platanus16-10Arbre1872-01-01T00:09:21+00:00Point[2.25294211329, 48.8671184424]NaN2.25294248.867118
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " datasetid recordid \\\n", "0 arbresremarquablesparis a236960f295288698ca57fdccfd9825547ffb4d0 \n", "1 arbresremarquablesparis d7635937c3636051c99963676d3a97dd4d9dd8c9 \n", "2 arbresremarquablesparis 460e8eccd3ebc816f16317702223bf406e7d75e6 \n", "3 arbresremarquablesparis b60b80e0f8327afba6d0c513b0b6b85639e516b6 \n", "4 arbresremarquablesparis 47e163d6f0536bc9e26ba2993447c6f50b14a6f6 \n", "\n", " record_timestamp fields.geom_x_y \\\n", "0 2019-10-04T10:00:41.063000+00:00 [48.8630096006, 2.2417737512] \n", "1 2019-10-04T10:00:41.063000+00:00 [48.8731110898, 2.24886478886] \n", "2 2019-10-04T10:00:41.063000+00:00 [48.8448875264, 2.35366916319] \n", "3 2019-10-04T10:00:41.063000+00:00 [48.8787029872, 2.30647417371] \n", "4 2019-10-04T10:00:41.063000+00:00 [48.8671184424, 2.25294211329] \n", "\n", " fields.libellefrancais fields.objectid fields.idemplacement \\\n", "0 Pin 49884 000901001 \n", "1 Hêtre 52761 000701004 \n", "2 Hêtre 62456 00000075 \n", "3 Erable 68801 00NO0144 \n", "4 Platane 82341 001201001 \n", "\n", " fields.arrondissement fields.circonferenceencm fields.hauteurenm ... \\\n", "0 BOIS DE BOULOGNE 241.0 30.0 ... \n", "1 BOIS DE BOULOGNE 231.0 16.0 ... \n", "2 PARIS 5E ARRDT 85.0 3.0 ... \n", "3 PARIS 8E ARRDT 450.0 28.0 ... \n", "4 BOIS DE BOULOGNE 534.0 45.0 ... \n", "\n", " fields.idbase fields.genre fields.complementadresse fields.typeemplacement \\\n", "0 2002349.0 Pinus 16-08 Arbre \n", "1 2002354.0 Fagus 16-17 Arbre \n", "2 132669.0 Fagus NaN Arbre \n", "3 305507.0 Acer 08-01 Arbre \n", "4 2002389.0 Platanus 16-10 Arbre \n", "\n", " fields.dateplantation geometry.type geometry.coordinates \\\n", "0 1882-01-01T00:09:21+00:00 Point [2.2417737512, 48.8630096006] \n", "1 1868-01-01T00:09:21+00:00 Point [2.24886478886, 48.8731110898] \n", "2 1905-01-01T00:09:21+00:00 Point [2.35366916319, 48.8448875264] \n", "3 1700-01-01T00:09:21+00:00 Point [2.30647417371, 48.8787029872] \n", "4 1872-01-01T00:09:21+00:00 Point [2.25294211329, 48.8671184424] \n", "\n", " fields.varieteoucultivar lon lat \n", "0 NaN 2.241774 48.863010 \n", "1 ''Pendula'' 2.248865 48.873111 \n", "2 ''Tortuosa'' 2.353669 48.844888 \n", "3 NaN 2.306474 48.878703 \n", "4 NaN 2.252942 48.867118 \n", "\n", "[5 rows x 26 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add longitude and latitude columns\n", "df['lon'] = df.apply(lambda row: row['geometry.coordinates'][0], axis=1)\n", "df['lat'] = df.apply(lambda row: row['geometry.coordinates'][1], axis=1)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datasetidrecordidrecord_timestampfields.geom_x_yfields.libellefrancaisfields.objectidfields.idemplacementfields.arrondissementfields.circonferenceencmfields.hauteurenm...fields.genrefields.complementadressefields.typeemplacementfields.dateplantationgeometry.typegeometry.coordinatesfields.varieteoucultivarlonlatgeometry
0arbresremarquablesparisa236960f295288698ca57fdccfd9825547ffb4d02019-10-04T10:00:41.063000+00:00[48.8630096006, 2.2417737512]Pin49884000901001BOIS DE BOULOGNE241.030.0...Pinus16-08Arbre1882-01-01T00:09:21+00:00Point[2.2417737512, 48.8630096006]NaN2.24177448.863010POINT (2.24177 48.86301)
1arbresremarquablesparisd7635937c3636051c99963676d3a97dd4d9dd8c92019-10-04T10:00:41.063000+00:00[48.8731110898, 2.24886478886]Hêtre52761000701004BOIS DE BOULOGNE231.016.0...Fagus16-17Arbre1868-01-01T00:09:21+00:00Point[2.24886478886, 48.8731110898]''Pendula''2.24886548.873111POINT (2.24886 48.87311)
2arbresremarquablesparis460e8eccd3ebc816f16317702223bf406e7d75e62019-10-04T10:00:41.063000+00:00[48.8448875264, 2.35366916319]Hêtre6245600000075PARIS 5E ARRDT85.03.0...FagusNaNArbre1905-01-01T00:09:21+00:00Point[2.35366916319, 48.8448875264]''Tortuosa''2.35366948.844888POINT (2.35367 48.84489)
3arbresremarquablesparisb60b80e0f8327afba6d0c513b0b6b85639e516b62019-10-04T10:00:41.063000+00:00[48.8787029872, 2.30647417371]Erable6880100NO0144PARIS 8E ARRDT450.028.0...Acer08-01Arbre1700-01-01T00:09:21+00:00Point[2.30647417371, 48.8787029872]NaN2.30647448.878703POINT (2.30647 48.87870)
4arbresremarquablesparis47e163d6f0536bc9e26ba2993447c6f50b14a6f62019-10-04T10:00:41.063000+00:00[48.8671184424, 2.25294211329]Platane82341001201001BOIS DE BOULOGNE534.045.0...Platanus16-10Arbre1872-01-01T00:09:21+00:00Point[2.25294211329, 48.8671184424]NaN2.25294248.867118POINT (2.25294 48.86712)
\n", "

5 rows × 27 columns

\n", "
" ], "text/plain": [ " datasetid recordid \\\n", "0 arbresremarquablesparis a236960f295288698ca57fdccfd9825547ffb4d0 \n", "1 arbresremarquablesparis d7635937c3636051c99963676d3a97dd4d9dd8c9 \n", "2 arbresremarquablesparis 460e8eccd3ebc816f16317702223bf406e7d75e6 \n", "3 arbresremarquablesparis b60b80e0f8327afba6d0c513b0b6b85639e516b6 \n", "4 arbresremarquablesparis 47e163d6f0536bc9e26ba2993447c6f50b14a6f6 \n", "\n", " record_timestamp fields.geom_x_y \\\n", "0 2019-10-04T10:00:41.063000+00:00 [48.8630096006, 2.2417737512] \n", "1 2019-10-04T10:00:41.063000+00:00 [48.8731110898, 2.24886478886] \n", "2 2019-10-04T10:00:41.063000+00:00 [48.8448875264, 2.35366916319] \n", "3 2019-10-04T10:00:41.063000+00:00 [48.8787029872, 2.30647417371] \n", "4 2019-10-04T10:00:41.063000+00:00 [48.8671184424, 2.25294211329] \n", "\n", " fields.libellefrancais fields.objectid fields.idemplacement \\\n", "0 Pin 49884 000901001 \n", "1 Hêtre 52761 000701004 \n", "2 Hêtre 62456 00000075 \n", "3 Erable 68801 00NO0144 \n", "4 Platane 82341 001201001 \n", "\n", " fields.arrondissement fields.circonferenceencm fields.hauteurenm ... \\\n", "0 BOIS DE BOULOGNE 241.0 30.0 ... \n", "1 BOIS DE BOULOGNE 231.0 16.0 ... \n", "2 PARIS 5E ARRDT 85.0 3.0 ... \n", "3 PARIS 8E ARRDT 450.0 28.0 ... \n", "4 BOIS DE BOULOGNE 534.0 45.0 ... \n", "\n", " fields.genre fields.complementadresse fields.typeemplacement \\\n", "0 Pinus 16-08 Arbre \n", "1 Fagus 16-17 Arbre \n", "2 Fagus NaN Arbre \n", "3 Acer 08-01 Arbre \n", "4 Platanus 16-10 Arbre \n", "\n", " fields.dateplantation geometry.type geometry.coordinates \\\n", "0 1882-01-01T00:09:21+00:00 Point [2.2417737512, 48.8630096006] \n", "1 1868-01-01T00:09:21+00:00 Point [2.24886478886, 48.8731110898] \n", "2 1905-01-01T00:09:21+00:00 Point [2.35366916319, 48.8448875264] \n", "3 1700-01-01T00:09:21+00:00 Point [2.30647417371, 48.8787029872] \n", "4 1872-01-01T00:09:21+00:00 Point [2.25294211329, 48.8671184424] \n", "\n", " fields.varieteoucultivar lon lat geometry \n", "0 NaN 2.241774 48.863010 POINT (2.24177 48.86301) \n", "1 ''Pendula'' 2.248865 48.873111 POINT (2.24886 48.87311) \n", "2 ''Tortuosa'' 2.353669 48.844888 POINT (2.35367 48.84489) \n", "3 NaN 2.306474 48.878703 POINT (2.30647 48.87870) \n", "4 NaN 2.252942 48.867118 POINT (2.25294 48.86712) \n", "\n", "[5 rows x 27 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from geopandas import GeoDataFrame, points_from_xy\n", "\n", "gdf = GeoDataFrame(df, geometry=points_from_xy(df['lon'], df['lat']))\n", "gdf.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " None\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", " Static map image\n", " \n", " \n", "
\n", "
\n", "
\n", " \n", " \n", "
\n", "
\n", "
\n", "\n", " \n", "\n", "
\n", "
\n", " :\n", "
\n", " \n", " \n", "
\n", "
\n", "\n", "
\n", " StackTrace\n", "
    \n", "
    \n", "
    \n", "\n", "\n", "\n", "\n", "\n", "\">\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cartoframes.viz import Layer\n", "\n", "Layer(gdf)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }