96 lines
3.0 KiB
Plaintext
96 lines
3.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Servidor 192.168.1.81 actualizado en coordenadas de mexico (pais)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import psycopg2\n",
|
|
"import json\n",
|
|
"from lst_servers import *\n",
|
|
"\n",
|
|
"# Parámetros de conexión a la base de datos\n",
|
|
"\n",
|
|
"json_file_path = \".\\GeoJSON - Censo 2010 (Municipal).json\"\n",
|
|
"with open(json_file_path, 'r', encoding='utf-8-sig') as f:\n",
|
|
" json_content = json.load(f)\n",
|
|
"\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",
|
|
" cursor = conn.cursor()\n",
|
|
" cursor.execute(\"DROP TABLE IF EXISTS coordenadas_mun;\")\n",
|
|
"\n",
|
|
" create_table_query = \"\"\"\n",
|
|
" CREATE TABLE coordenadas_mun (\n",
|
|
" id_edo INT,\n",
|
|
" edo CHARACTER VARYING,\n",
|
|
" id_mun INT,\n",
|
|
" mun CHARACTER VARYING,\n",
|
|
" key_edomun CHARACTER VARYING,\n",
|
|
" geojson_mun JSONB\n",
|
|
" );\"\"\"\n",
|
|
"\n",
|
|
" cursor.execute(create_table_query)\n",
|
|
"\n",
|
|
" # Insertar el contenido JSON en la tabla\n",
|
|
" \n",
|
|
" for index in enumerate(json_content['features']):\n",
|
|
" id_edo = int(json_content['features'][index[0]]['properties']['cve_ent'])\n",
|
|
" edo = json_content['features'][index[0]]['properties']['nom_ent']\n",
|
|
" id_mun = int(json_content['features'][index[0]]['properties']['cve_umun'][2:])\n",
|
|
" mun = json_content['features'][index[0]]['properties']['nom_mun']\n",
|
|
" key_edomun = f'{id_edo}_{id_mun}'\n",
|
|
" geometry = json_content['features'][index[0]]['geometry']\n",
|
|
" insert_query = \"INSERT INTO coordenadas_mun (id_edo, edo, id_mun, mun, key_edomun, geojson_mun ) VALUES (%s, %s, %s, %s, %s, %s);\"\n",
|
|
" cursor.execute(insert_query, (id_edo, edo, id_mun, mun, key_edomun, json.dumps(geometry),))\n",
|
|
" \n",
|
|
" # insert_query = \"INSERT INTO coordenadas_mun (json_data) VALUES (%s);\"\n",
|
|
" # cursor.execute(insert_query, (json.dumps(json_content),))\n",
|
|
"\n",
|
|
" # guardar cambios\n",
|
|
" conn.commit()\n",
|
|
" # Cerrar la conexión\n",
|
|
" cursor.close()\n",
|
|
" conn.close()\n",
|
|
" print(f'Servidor {ip} actualizado en coordenadas de mexico (pais)')\n"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|