{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting coordinates between projections"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are some made-up coordinates, UTM zone 16N, NAD27 datum:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x = 1796891.94\n",
"y = 101128551.19"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'd like to convert them into UTM zone 15N, WGS84 datum.\n",
"\n",
"For a start, those look like they are in feet. Let's convert them:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(547692.663312, 30823982.402712002)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x *= 0.3048\n",
"y *= 0.3048\n",
"x, y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will use the [`pyproj`](https://pypi.python.org/pypi/pyproj) project. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import pyproj as pp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set up the 'source' and 'target' projections using [EPSG codes](http://spatialreference.org/):"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"utm16_nad27 = pp.Proj(init='epsg:26716')\n",
"utm15_wgs84 = pp.Proj(init='epsg:32615') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can do the transformation..."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"new_coords = pp.transform(utm16_nad27, # From\n",
" utm15_wgs84, # To\n",
" x, y) # Coordinates"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(633989.9634796401, -9177126.336612783)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_coords # These are in metres!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Other formats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `x` and `y` could also be arrays:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1147639.24836805, 1196558.79719467, 1206342.16627149]),\n",
" array([1608596.47150905, 1660201.86925774, 1670532.61011427]))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"x = np.array([500000, 550000, 560000])\n",
"y = np.array([1600000, 1650000, 1660000])\n",
"\n",
"pp.transform(utm16_nad27,utm15_wgs84, x, y) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have an array of (x, y) pairs, there's a quick way to unpack them:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1147639.24836805, 1196558.79719467, 1206342.16627149]),\n",
" array([1608596.47150905, 1660201.86925774, 1670532.61011427]))"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coords = np.array([[500000, 1600000], [550000, 1650000], [560000, 1660000]])\n",
"\n",
"pp.transform(utm16_nad27,utm15_wgs84, *coords.T) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or you might have a `pandas` DataFrame with `x` and `y` columns:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.DataFrame(coords, columns=['x_UTM16', 'y_UTM16'])\n",
"df['Attrib1'] = [123, 456, 789]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" x | \n",
" y | \n",
" Attrib1 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 500000 | \n",
" 1600000 | \n",
" 123 | \n",
"
\n",
" \n",
" 1 | \n",
" 550000 | \n",
" 1650000 | \n",
" 456 | \n",
"
\n",
" \n",
" 2 | \n",
" 560000 | \n",
" 1660000 | \n",
" 789 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" x y Attrib1\n",
"0 500000 1600000 123\n",
"1 550000 1650000 456\n",
"2 560000 1660000 789"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"df['x_UTM15'], df['y_UTM15'] = pp.transform(utm16_nad27,utm15_wgs84, df.x.values, df.y.values) "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" x | \n",
" y | \n",
" Attrib1 | \n",
" x_UTM15 | \n",
" y_UTM15 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 500000 | \n",
" 1600000 | \n",
" 123 | \n",
" 1.147639e+06 | \n",
" 1.608596e+06 | \n",
"
\n",
" \n",
" 1 | \n",
" 550000 | \n",
" 1650000 | \n",
" 456 | \n",
" 1.196559e+06 | \n",
" 1.660202e+06 | \n",
"
\n",
" \n",
" 2 | \n",
" 560000 | \n",
" 1660000 | \n",
" 789 | \n",
" 1.206342e+06 | \n",
" 1.670533e+06 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" x y Attrib1 x_UTM15 y_UTM15\n",
"0 500000 1600000 123 1.147639e+06 1.608596e+06\n",
"1 550000 1650000 456 1.196559e+06 1.660202e+06\n",
"2 560000 1660000 789 1.206342e+06 1.670533e+06"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"pd.options.display.float_format = '{:.0f}'.format"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" x | \n",
" y | \n",
" Attrib1 | \n",
" x_UTM15 | \n",
" y_UTM15 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 500000 | \n",
" 1600000 | \n",
" 123 | \n",
" 1147639 | \n",
" 1608596 | \n",
"
\n",
" \n",
" 1 | \n",
" 550000 | \n",
" 1650000 | \n",
" 456 | \n",
" 1196559 | \n",
" 1660202 | \n",
"
\n",
" \n",
" 2 | \n",
" 560000 | \n",
" 1660000 | \n",
" 789 | \n",
" 1206342 | \n",
" 1670533 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" x y Attrib1 x_UTM15 y_UTM15\n",
"0 500000 1600000 123 1147639 1608596\n",
"1 550000 1650000 456 1196559 1660202\n",
"2 560000 1660000 789 1206342 1670533"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"© 2019 Agile Geoscience — CC-BY — Have fun!
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "geocomp",
"language": "python",
"name": "geocomp"
},
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}