Repository: geopandas/geopandas

Issue:
to_crs doesn't take _geometry_column_name into account
When I have geodataframe with geometry column named e.g. 'geom', and convert the dataframe to another crs, new column named 'geometry' is created.

Is this intentional? I think that more predictable behaviour would be to rewrite the old geometry column instead of creating new one.

(Using version 0.2.0.dev cloned form this repo.)



Discussion:
This is due to a bug in `set_geometry()`. It can be reproduced with `df.set_geometry(df.geometry)`, which will introduce a new `geometry` column for any `GeoDataFrame` using a different geometry column name.


Resolving patch:
diff --git a/geopandas/geodataframe.py b/geopandas/geodataframe.py
--- a/geopandas/geodataframe.py
+++ b/geopandas/geodataframe.py
@@ -125,7 +125,7 @@ def set_geometry(self, col, drop=False, inplace=False, crs=None):
             crs = getattr(col, 'crs', self.crs)
 
         to_remove = None
-        geo_column_name = DEFAULT_GEO_COLUMN_NAME
+        geo_column_name = self._geometry_column_name
         if isinstance(col, (Series, list, np.ndarray)):
             level = col
         elif hasattr(col, 'ndim') and col.ndim != 1:
@@ -139,7 +139,7 @@ def set_geometry(self, col, drop=False, inplace=False, crs=None):
                 raise
             if drop:
                 to_remove = col
-                geo_column_name = DEFAULT_GEO_COLUMN_NAME
+                geo_column_name = self._geometry_column_name
             else:
                 geo_column_name = col
 


Test patch:
diff --git a/geopandas/tests/test_geodataframe.py b/geopandas/tests/test_geodataframe.py
--- a/geopandas/tests/test_geodataframe.py
+++ b/geopandas/tests/test_geodataframe.py
@@ -55,16 +55,12 @@ def test_different_geo_colname(self):
 
         geom2 = [Point(x, y) for x, y in zip(range(5, 10), range(5))]
         df2 = df.set_geometry(geom2, crs='dummy_crs')
-        self.assert_('geometry' in df2)
         self.assert_('location' in df2)
         self.assertEqual(df2.crs, 'dummy_crs')
         self.assertEqual(df2.geometry.crs, 'dummy_crs')
         # reset so it outputs okay
         df2.crs = df.crs
         assert_geoseries_equal(df2.geometry, GeoSeries(geom2, crs=df2.crs))
-        # for right now, non-geometry comes back as series
-        assert_geoseries_equal(df2['location'], df['location'],
-                                  check_series_type=False, check_dtype=False)
 
     def test_geo_getitem(self):
         data = {"A": range(5), "B": range(-5, 0),
@@ -372,6 +368,18 @@ def test_transform(self):
         utm = lonlat.to_crs(epsg=26918)
         self.assertTrue(all(df2['geometry'].geom_almost_equals(utm['geometry'], decimal=2)))
 
+    def test_to_crs_geo_column_name(self):
+        # Test to_crs() with different geometry column name (GH#339)
+        df2 = self.df2.copy()
+        df2.crs = {'init': 'epsg:26918', 'no_defs': True}
+        df2 = df2.rename(columns={'geometry': 'geom'})
+        df2.set_geometry('geom', inplace=True)
+        lonlat = df2.to_crs(epsg=4326)
+        utm = lonlat.to_crs(epsg=26918)
+        self.assertEqual(lonlat.geometry.name, 'geom')
+        self.assertEqual(utm.geometry.name, 'geom')
+        self.assertTrue(all(df2.geometry.geom_almost_equals(utm.geometry, decimal=2)))
+
     def test_from_features(self):
         nybb_filename, nybb_zip_path = download_nybb()
         with fiona.open(nybb_zip_path,