Code Monkey home page Code Monkey logo

Comments (12)

jdmcbr avatar jdmcbr commented on May 19, 2024 6

@bidhya Ah, I see what you mean. The current to_csv function in geopandas, including my branch above, is just the pandas function. It hasn't been overridden with a GeoDataFrame-specific method. So going in a to_csv/from_csv cycle will result in the behavior you describe. I think a GeoDataFrame.to_csv method would be good though. Had there been more activity in this thread after my first comment, six months ago, I would have attempted such a method and submitted a PR.

from geopandas.

carsonfarmer avatar carsonfarmer commented on May 19, 2024 1

I'd also like to see a from_csv method in this same vein. The only potential 'issue' is that the Pandas API allows for returning a text streamer, which will require a bit more thinking/wrapping.

from geopandas.

carsonfarmer avatar carsonfarmer commented on May 19, 2024

Actually, OGR does support CSV files with WKT geometries... so you could output geometries other than points...

from geopandas.

jwass avatar jwass commented on May 19, 2024

Ah. I've never seen non-point CSVs in the wild but I have a pretty limited view.

Maybe make arguments x and y optional? If they're specified, then it would behave as above and assume points. Otherwise output WKT...?

from geopandas.

scw avatar scw commented on May 19, 2024

I think it'd be great to optionally output VRT files alongside the CSVs. The VRT is a simple XML document which can be read by OGR to infer the geographic data contained in a CSV, and makes interoperating with CSVs much easier. See this GIS.SE question for a look at the approach, or the CSV driver docs.

from geopandas.

jdmcbr avatar jdmcbr commented on May 19, 2024

I am also interested in to_csv/from_csv methods. I wrote a little from_csv function (which wraps a from_dataframe function) in my fork (ddefbd4). It makes no attempt to handle polygons, and requires that the column names for x/y be specified. It works fine for my purposes, but I have not tested rigorously.

(Edited SHA after switching commit to a new branch; I forgot that commits get automatically included in existing pull requests.)

from geopandas.

bidhya avatar bidhya commented on May 19, 2024

So it is possible to use from_csv to load the saved geodataframe without any hack?

from geopandas.

jdmcbr avatar jdmcbr commented on May 19, 2024

I'm not quite sure what you mean by "without any hack". Assuming a csv file with points whose columns are labeled longitude and latitude, it would create a GeoDataFrame with the following single call:

>>> gdf = geopandas.GeoDataFrame.from_csv("filename.csv", "longitude", "latitude")

However, I never made a pull request, since this issue was already here, so my implementation is not currently part of geopandas.

from geopandas.

bidhya avatar bidhya commented on May 19, 2024

@jdmcbr In geopandas, the geometry information is embedded in the column names "geomtry". When I save this geodataframe, this geometry column is saved as well (there is no explicit logitude and latitude columns).

So when I load this csv file, I expect it to work exactly like the original geodataframe that saved this. But this is not how it works. For example, I load the saved dataframe as follows

gdf = geopandas.GeoDataFrame.from_csv("filename.csv")
Now using a plot command will plot the numerical columns like regular pandas dataframe. If gdf we true geodataframe, it should have plotted the geometry only.

Thats what I mean by "without a hack". When you load a shapefile with geopandas.read_file('filename.shp') there is no column called longitude and latitude, only a column called geometry. As soon as I save this geodataframe to csv and load again, it stops behaving as a geodataframe.

from geopandas.

githubtianfeng avatar githubtianfeng commented on May 19, 2024

@jwass @jdmcbr May I ask if this problem was resolved later?I also encountered a similar problem.

Expected behavior and actual behavior.

File "/home/work/extoanaservice/app/analyzers/work_area.py", line 627, in init
mesh_link_gdf.to_csv(os.path.join(self.file_manager.storage_folder(), mesh_id + '.csv'), index=False)
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/pandas/core/frame.py", line 1745, in to_csv
formatter.save()
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/pandas/io/formats/csvs.py", line 151, in save
self._save()
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/pandas/io/formats/csvs.py", line 263, in _save
self._save_chunk(start_i, end_i)
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/pandas/io/formats/csvs.py", line 277, in _save_chunk
quoting=self.quoting)
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/pandas/core/internals.py", line 766, in to_native_types
values = values.astype(str)
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/shapely/geometry/base.py", line 238, in str
return self.wkt
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/shapely/geometry/base.py", line 367, in wkt
return WKTWriter(lgeos, **kw).write(self)
File "/home/work/extoanaservice/Python-2.7.14-env/lib/python2.7/site-packages/shapely/geos.py", line 337, in init
self._writer = self._lgeos.GEOSWKTWriter_create()
ArgumentError: argument 1: <class 'ctypes.ArgumentError'>: argument 1: <class 'ctypes.ArgumentError'>: ......

There is a certain probability that this problem will occur.

Shapely version and provenance

Shapely==1.6.4.post1 fiona==1.8.22 pandas==0.23.0 geopandas==0.6.0
libgeos 3.6.2

from geopandas.

martinfleis avatar martinfleis commented on May 19, 2024

to_csv has not been implemented. The recommended, geometry agnostic way, is to cast geometries to WKT before saving as csv.

gdf.to_wkt().to_csv()

from geopandas.

martinfleis avatar martinfleis commented on May 19, 2024

@githubtianfeng Please see the answers in the shapely repository regarding your environment issues. None is f the versions you have is supported any longer.

from geopandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.