{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# convert coordinates of fiona objects (JSON) from one coordinate system to another" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'geometry': {'type': 'Point', 'coordinates': (39.22, -6.81)}}\n" ] } ], "source": [ "import fiona\n", "from fiona.crs import from_epsg, to_string\n", "from shapely.geometry import Point, mapping\n", "import pyproj\n", "\n", "xmin, ymin = (39.22, -6.81)\n", "rounding = 0\n", "\n", "geom = Point((xmin, ymin))\n", "\n", "feat = {'geometry': mapping(geom)}\n", "trg = pyproj.Proj(to_string(from_epsg(4326)))\n", "src = pyproj.Proj(to_string(from_epsg(4326)))\n", "\n", "def proj_feat(feat, src, trg, rounding=0., round_x='up', round_y='up'):\n", " def roundup(x, rounding=0.):\n", " if rounding == 0.:\n", " return x\n", " else:\n", " return x if x % rounding == 0 else x + rounding - x % rounding\n", "\n", " def rounddown(x, rounding=0.):\n", " if rounding==0.:\n", " return x\n", " else:\n", " return x if x % rounding == 0 else x - x % rounding\n", " _x, _y = feat['geometry']['coordinates']\n", " _x_proj, _y_proj = pyproj.transform(src, trg, _x, _y)\n", " if round_x == 'up':\n", " x = roundup(_x_proj, rounding=rounding)\n", " else:\n", " x = rounddown(_x_proj, rounding=rounding)\n", " if round_y == 'up':\n", " y = roundup(_y_proj, rounding=rounding)\n", " else:\n", " y = rounddown(_y_proj, rounding=rounding)\n", " # make feat\n", " feat['geometry']['coordinates'] = (x, y)\n", " return feat\n", "print proj_feat(feat, src, trg, rounding=rounding)\n", "# print _x_proj, roundup(_x_proj, 1000), rounddown(_x_proj, 1000)\n", "\n", "# print feat['geometry']['coordinates']\n", "\n", "# original = Proj(shape.crs) # EPSG:4326 in your case\n", "# destination = Proj('+proj=lcc +lat_1=36.41666666666666 +lat_2=35.25 +lat_0=34.33333333333334 +lon_0=-86 +x_0=600000 +y_0=0 +ellps=GRS80 +datum=NAD83 +no_defs')\n", "# with fiona.open('new.shp', 'w', 'ESRI Shapefile',shape.schema.copy(),crs = dest_crs) as output:\n", "# for feat in shape: \n", "# print feat['geometry']['coordinates']\n", "# for counter in range(0,len(feat['geometry']['coordinates'][0])):\n", "# long,lat = feat['geometry']['coordinates'][0][counter]\n", "# x,y = transform(original, destination,long,lat)\n", "# feat['geometry']['coordinates'][0][counter] = (x,y)\n", "# output.write(feat)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'+init=epsg:32737 +no_defs'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "to_string(src)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyproj\n", "wgs84=pyproj.Proj(\"+init=EPSG:4326\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }