Skip to content

Instantly share code, notes, and snippets.

@ocefpaf
Created June 24, 2022 13:21
Show Gist options
  • Save ocefpaf/83a83c506bd69faf453e21bbdb02fb7f to your computer and use it in GitHub Desktop.
Save ocefpaf/83a83c506bd69faf453e21bbdb02fb7f to your computer and use it in GitHub Desktop.
pyproj_examples
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://pyproj4.github.io/pyproj/stable/examples.html?highlight=utm#find-utm-crs-by-latitude-and-longitude"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd\nimport io\n\n\ntext = \"\"\"\nUTMx,UTMy,Lon,Lat\n529025,7422210,-44.716128,-23.309315\n529114,7422343,-44.715260,-23.308112\n545227,7435702,-44.558073,-23.187077\n545582,7435741,-44.554606,-23.186714\n\"\"\"\n\nwith io.StringIO(text) as f:\n df = pd.read_csv(f)",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": " UTMx UTMy Lon Lat\n0 529025 7422210 -44.716128 -23.309315\n1 529114 7422343 -44.715260 -23.308112\n2 545227 7435702 -44.558073 -23.187077\n3 545582 7435741 -44.554606 -23.186714",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>UTMx</th>\n <th>UTMy</th>\n <th>Lon</th>\n <th>Lat</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>529025</td>\n <td>7422210</td>\n <td>-44.716128</td>\n <td>-23.309315</td>\n </tr>\n <tr>\n <th>1</th>\n <td>529114</td>\n <td>7422343</td>\n <td>-44.715260</td>\n <td>-23.308112</td>\n </tr>\n <tr>\n <th>2</th>\n <td>545227</td>\n <td>7435702</td>\n <td>-44.558073</td>\n <td>-23.187077</td>\n </tr>\n <tr>\n <th>3</th>\n <td>545582</td>\n <td>7435741</td>\n <td>-44.554606</td>\n <td>-23.186714</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from pyproj import CRS\nfrom pyproj.aoi import AreaOfInterest\nfrom pyproj.database import query_utm_crs_info\n\n\nutm_crs_list = query_utm_crs_info(\n datum_name=\"WGS 84\", # Tem que ser o mesmo do sua coleta de dados, check o setting do seu GPS\n area_of_interest=AreaOfInterest(\n west_lon_degree=df[\"Lon\"].min(),\n south_lat_degree=df[\"Lat\"].min(),\n east_lon_degree=df[\"Lon\"].max(),\n north_lat_degree=df[\"Lon\"].max(),\n ),\n)\n\nutm_crs = CRS.from_epsg(utm_crs_list[0].code)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Checa a Zona UTM e a área de uso para ter certeza que escolheu os limites corretos.\n\nÁreas muito grandes pode conter múltiplas zonas!"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "utm_crs",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "<Derived Projected CRS: EPSG:32723>\nName: WGS 84 / UTM zone 23S\nAxis Info [cartesian]:\n- E[east]: Easting (metre)\n- N[north]: Northing (metre)\nArea of Use:\n- name: Between 48°W and 42°W, southern hemisphere between 80°S and equator, onshore and offshore. Brazil.\n- bounds: (-48.0, -80.0, -42.0, 0.0)\nCoordinate Operation:\n- name: UTM zone 23S\n- method: Transverse Mercator\nDatum: World Geodetic System 1984 ensemble\n- Ellipsoid: WGS 84\n- Prime Meridian: Greenwich"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Convertendo de epsg:4326 [1] para UTM. Note que você precisa saber o datum dos seus dados! Não assuma epsg:4326!!\n\n1. https://epsg.io/4326\n"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from pyproj import Transformer\n\n\ncrs_4326 = CRS.from_epsg(4326)\nproj = Transformer.from_crs(crs_4326, utm_crs)\n\nproj.transform(xx=df[\"Lon\"], yy=df[\"Lat\"])",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "(array([2217536.82079295, 2217658.86540163, 2232129.9515884 ,\n 2232266.74451289]),\n array([4814180.97526696, 4814250.59013185, 4828994.00324645,\n 4829370.44367393]))"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Fazendo o inverso."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "proj = Transformer.from_crs(utm_crs, crs_4326)\n\ny, x = proj.transform(xx=df[\"UTMx\"], yy=df[\"UTMy\"])\n\ny, x",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "(array([-23.3093153 , -23.30811233, -23.18707651, -23.18671445]),\n array([-44.71612809, -44.71526021, -44.55807312, -44.55460555]))"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Checar se está OK até 6 casas decimais."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\n\n\nnp.testing.assert_array_almost_equal(x, df[\"Lon\"], decimal=6)\nnp.testing.assert_array_almost_equal(y, df[\"Lat\"], decimal=6)",
"execution_count": 7,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.10.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "pyproj_examples",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment