{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:11:36.222965Z", "iopub.status.busy": "2022-09-15T05:11:36.222553Z", "iopub.status.idle": "2022-09-15T05:11:37.711425Z", "shell.execute_reply": "2022-09-15T05:11:37.710585Z" } }, "outputs": [], "source": [ "import starepandas\n", "import geopandas\n", "import sqlalchemy\n", "import pandas\n", "import numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Prep" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:11:37.714815Z", "iopub.status.busy": "2022-09-15T05:11:37.714143Z", "iopub.status.idle": "2022-09-15T05:11:37.727553Z", "shell.execute_reply": "2022-09-15T05:11:37.726795Z" } }, "outputs": [], "source": [ "cities = ['Buenos Aires', 'Brasilia', 'Santiago', \n", " 'Bogota', 'Caracas', 'Sao Paulo', 'Bridgetown']\n", "\n", "latitudes = [-34.58, -15.78, -33.45, 4.60, 10.48, -23.55, 13.1]\n", "longitudes = [-58.66, -47.91, -70.66, -74.08, -66.86, -46.63, -59.62]\n", "data = {'City': cities, \n", " 'Latitude': latitudes, 'Longitude': longitudes}\n", "\n", "cities = starepandas.STAREDataFrame(data)\n", "sids = starepandas.sids_from_xy(cities.Longitude, cities.Latitude, level=27)\n", "cities.set_sids(sids, inplace=True)\n", "geom = geopandas.points_from_xy(cities.Longitude, cities.Latitude, crs='EPSG:4326')\n", "cities.set_geometry(geom, inplace=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# To SQLite" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:11:37.739722Z", "iopub.status.busy": "2022-09-15T05:11:37.738932Z", "iopub.status.idle": "2022-09-15T05:11:37.813204Z", "shell.execute_reply": "2022-09-15T05:11:37.812210Z" } }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db_path = '' # Empty string for inmemory\n", "uri = 'sqlite:///{db_path}'.format(db_path=db_path)\n", "engine = sqlalchemy.create_engine(uri)\n", "\n", "cities = geopandas.io.sql._convert_to_ewkb(cities, 'geometry', 4326)\n", "cities.to_sql(name='cities', con=engine, if_exists='replace')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# From SQLite" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:11:37.817817Z", "iopub.status.busy": "2022-09-15T05:11:37.817307Z", "iopub.status.idle": "2022-09-15T05:11:37.850594Z", "shell.execute_reply": "2022-09-15T05:11:37.849552Z" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | index | \n", "City | \n", "Latitude | \n", "Longitude | \n", "sids | \n", "geometry | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "0 | \n", "Buenos Aires | \n", "-34.58 | \n", "-58.66 | \n", "2663379193440875387 | \n", "0101000020E610000014AE47E17A544DC00AD7A3703D4A... | \n", "
| 1 | \n", "1 | \n", "Brasilia | \n", "-15.78 | \n", "-47.91 | \n", "2867415364672350651 | \n", "0101000020E610000014AE47E17AF447C08FC2F5285C8F... | \n", "
| 2 | \n", "2 | \n", "Santiago | \n", "-33.45 | \n", "-70.66 | \n", "2723774768829278555 | \n", "0101000020E61000000AD7A3703DAA51C09A99999999B9... | \n", "
| 3 | \n", "3 | \n", "Bogota | \n", "4.60 | \n", "-74.08 | \n", "2667981979956219515 | \n", "0101000020E610000085EB51B81E8552C0666666666666... | \n", "
| 4 | \n", "4 | \n", "Caracas | \n", "10.48 | \n", "-66.86 | \n", "2494081632617553403 | \n", "0101000020E6100000D7A3703D0AB750C0F6285C8FC2F5... | \n", "
| 5 | \n", "5 | \n", "Sao Paulo | \n", "-23.55 | \n", "-46.63 | \n", "2803225788975740475 | \n", "0101000020E6100000713D0AD7A35047C0CDCCCCCCCC8C... | \n", "
| 6 | \n", "6 | \n", "Bridgetown | \n", "13.10 | \n", "-59.62 | \n", "2518254660685127707 | \n", "0101000020E61000008FC2F5285CCF4DC0333333333333... | \n", "