Comments (12)
@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.
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.
Actually, OGR does support CSV files with WKT
geometries... so you could output geometries other than points...
from geopandas.
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.
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.
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.
So it is possible to use from_csv
to load the saved geodataframe without any hack?
from geopandas.
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.
@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.
@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.
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.
@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)
- GeoPandas 1.0 release HOT 11
- MAINT: Fiona 1.10 support HOT 4
- BUG: up to 4 times slower in Linux compared to Windows when using gpd.read_file to read vector data HOT 6
- BUG: Is it possible to use GeoPandas on Windows 7 ?
- BUG: Unable to append to a layer when using fiona backend HOT 3
- BUG: plotting_with_geoplot example is broken HOT 1
- ENH: Add a Voronoi Diagram algorithm HOT 1
- BUG: The dissolve feature generates a new geometry HOT 4
- REGR: incorrect order of left sjoin with within predicate HOT 1
- BUG: wrong foxpro DBF file read HOT 7
- Fiona 1.10a2 issues HOT 2
- ENH: support writing + filtered reading from bbox columns in GeoParquet HOT 4
- ENH: support reading and writing the geoarrow-based encodings of GeoParquet
- API: Series alignment for non binary op methods HOT 5
- PERF: optimize `==` and `!=` for GeoSeries (`GeometryArray.__eq__`)
- numpy dependency missing & numpy 2.0 support status? HOT 3
- ENH: inplace=True for .dissolve and .explode? HOT 4
- BUG: `pyogrio` doesn't like io.BytesIO? HOT 10
- BUG: Issue with webpage code interp HOT 1
- ENH: sjoin() should allow to return the distance when using the dwithin predicate, just like sjoin_nearest() HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geopandas.