sict_csic/help_files/coordenadas/carga_locs.ipynb

151 lines
5.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 192244/192244 [20:23<00:00, 157.09it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Servidor 192.168.1.81 actualizado en coordenadas de mexico (pais)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"import psycopg2\n",
"import json\n",
"from lst_servers import *\n",
"from tqdm import tqdm\n",
"\n",
"# Parámetros de conexión a la base de datos\n",
"\n",
"json_file_path = \"./GeoJSON - Censo 2010 (Localidad) ITER.json\"\n",
"\n",
"with open(json_file_path, 'r', encoding='utf-8-sig') as f:\n",
" json_content = json.load(f)\n",
"\n",
"# Divide los datos en bloques más pequeños\n",
"chunk_size = 100 # Ajusta el tamaño del bloque según tus necesidades\n",
"json_features = json_content['features']\n",
"\n",
"chunks = [json_features[i:i + chunk_size] for i in range(0, len(json_features), chunk_size)]\n",
"\n",
"# Itera sobre los servidores e inserta los bloques de datos en la base de datos\n",
"for ip in l_ips:\n",
" conn = psycopg2.connect(\n",
" host=ip,\n",
" port=5432,\n",
" database=\"coordenadas_mx\",\n",
" user=\"postgres\",\n",
" password=\"Shala55951254\"\n",
" )\n",
" \n",
" cursor = conn.cursor()\n",
" cursor.execute(\"DROP TABLE IF EXISTS coordenadas_loc;\")\n",
"\n",
" create_table_query = \"\"\"\n",
" CREATE TABLE coordenadas_loc (\n",
" key_edomunloc CHARACTER VARYING,\n",
" id_edo CHARACTER VARYING,\n",
" edo CHARACTER VARYING,\n",
" id_mun CHARACTER VARYING, \n",
" mun CHARACTER VARYING,\n",
" id_loc CHARACTER VARYING,\n",
" loc CHARACTER VARYING,\n",
" json_data JSONB\n",
" );\"\"\"\n",
" \n",
" cursor.execute(create_table_query)\n",
" \n",
" # for i in tqdm(range(len(json_content['features']))):\n",
" # key_edomunloc = f\"{json_content['features'][i]['properties']['entidad']}_{json_content['features'][i]['properties']['mun']}_{json_content['features'][i]['properties']['loc']}\"\n",
" # id_edo = json_content['features'][i]['properties']['entidad']\n",
" # edo = json_content['features'][i]['properties']['nom_ent']\n",
" # id_mun = json_content['features'][i]['properties']['mun']\n",
" # mun = json_content['features'][i]['properties']['nom_mun']\n",
" # id_loc = json_content['features'][i]['properties']['loc']\n",
" # loc = json_content['features'][i]['properties']['nom_loc']\n",
" # geometry = json.dumps({'feactures': json_content['features'][i]['geometry']})\n",
" for i in tqdm(range(len(json_content['features']))):\n",
" key_edomunloc = f\"{json_content['features'][i]['properties']['entidad']}_{json_content['features'][i]['properties']['mun']}_{json_content['features'][i]['properties']['loc']}\"\n",
" id_edo = json_content['features'][i]['properties']['entidad']\n",
" edo = json_content['features'][i]['properties']['nom_ent']\n",
" id_mun = json_content['features'][i]['properties']['mun']\n",
" mun = json_content['features'][i]['properties']['nom_mun']\n",
" id_loc = json_content['features'][i]['properties']['loc']\n",
" loc = json_content['features'][i]['properties']['nom_loc']\n",
" geometry = json.dumps(json_content['features'][i]['geometry']) # Remove redundant dictionary\n",
"\n",
" insert_query = \"INSERT INTO coordenadas_loc (key_edomunloc, id_edo, edo, id_mun, mun, id_loc, loc, json_data) VALUES (%s, %s, %s, %s, %s, %s, %s, %s);\"\n",
"\n",
" cursor.execute(insert_query, (key_edomunloc, id_edo, edo, id_mun, mun, id_loc, loc, geometry))\n",
" conn.commit()\n",
"\n",
" # -------\n",
"\n",
"\n",
" # Guardar cambios\n",
" \n",
" # Cerrar la conexión\n",
" cursor.close()\n",
" conn.close()\n",
" print(f'Servidor {ip} actualizado en coordenadas de mexico (pais)')\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# dict_keys(['type', 'id', 'geometry', 'geometry_name', 'properties'])\n",
"\n",
"id_edo = json_content['features'][0]['properties']['entidad']\n",
"edo = json_content['features'][0]['properties']['nom_ent']\n",
"id_mun = json_content['features'][0]['properties']['mun']\n",
"mun = json_content['features'][0]['properties']['nom_mun']\n",
"id_loc = json_content['features'][0]['properties']['loc']\n",
"loc = json_content['features'][0]['properties']['nom_loc']\n",
"geometry = json_content['features'][0]['geometry']"
]
}
],
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}