This question is related to Converting shapefiles to text (ASCII) files?.
I have a CSV file, with one column, where all the rows correspond to WKT POLYGON()'s:
WKT
POLYGON(...)
POLYGON(...)
...
I'm familiar with how to convert from shapefile to wkt, but I need to go the other way around. How to accomplish this task?
I tried playing around with ogr2ogr
's settings/flags but didn't really get anything useful.
I also know I can use QGIS to do this, but it freezes/crashes since the dataset is fairly large.
3 Answers 3
I had to solve the same problem today, so here is my answer, which gives a complete solution.
I have a lineWKT.csv
file stored in F:\Data\
folder, with the data like this:
id,gm
0,"LINESTRING (30 10 0, 10 30 0, 40 40 5)"
I have a test.vrt
file like this:
<OGRVRTDataSource>
<OGRVRTLayer name="lineWKT">
<SrcDataSource>F:\Data\lineWKT.csv</SrcDataSource>
<GeometryType>wkbLineString25D</GeometryType>
<LayerSRS>PROJCS["WGS_1984_Lambert_Conformal_Conic",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1000000.0],PARAMETER["False_Northing",1000000.0],PARAMETER["Central_Meridian",85.875],PARAMETER["Standard_Parallel_1",24.625],PARAMETER["Standard_Parallel_2",27.125],PARAMETER["Latitude_Of_Origin",25.8772525],UNIT["Meter",1.0]]</LayerSRS>
<GeometryField encoding="WKT" field='gm' > </GeometryField >
</OGRVRTLayer>
</OGRVRTDataSource>
With this configuration, I can create a shapefile with the following command:
ogr2ogr line.shp test.vrt
On Linux/Ubuntu, I needed to do something similar today and came up with the following solution. It uses a few helper utilities that were already in my Ubuntu shell, but it's an effective one-liner that doesn't require creating any VRT sidecar, etc. It's a handy way to solve "I have some WKT features and I want a shapefile" without having to open any software.
In my case, I didn't have a CSV yet, I just had a couple of coordinate pairs. So this command 1) creates a fresh file, "dataset.csv" 2) pushes in some content, specifically three lines of a header, and two features, and 3) uses ogr2ogr to export the source CSV as a shapefile in WGS84.
touch dataset.csv && \
printf "gid,WKT\n1,POINT(-82.048051 33.567181)\n2,POINT(-92.7774 35.9829)\n" > dataset.csv && \
ogr2ogr -f "ESRI Shapefile" dataset.shp -dialect sqlite \
-sql "SELECT gid, GeomFromText(WKT) FROM dataset" dataset.csv -a_srs EPSG:4326
- Create the file:
touch dataset.csv
- Push some content into the file through STDOUT
printf "gid,WKT\n1,POINT(-82.048051 33.567181)\n2,POINT(-92.7774 35.9829)\n" > pour_pt_4.csv
- Create the SHP (
ogr2ogr -f "ESRI Shapefile" dataset.shp
) by reading the source CSV using a precision SQL expression within ogr2ogr. The SQL expression essentially ensures that ogr2ogr properly recognizes the geometry column in the source CSV. - Of note but somewhat elementary, the
-a_srs
parameter ensure that ogr2ogr recognizes the source CSV as WGS84 (EPSG:4326) so that this SRS will propagate to the resulting shapefile.
-
It just occurred to me ..if you don't want to leave the residual
CSV
file hanging around, you could add a trailing command to take out the garbage. Just append this to the command:&& rm -f dataset.csv
elrobis– elrobis2020年07月17日 14:15:17 +00:00Commented Jul 17, 2020 at 14:15 -
1If you have data columns that you want to include using the correct data type, you can use
CAST AS
in the SQL statement:-sql "SELECT CAST(id AS int) AS id, CAST(x AS decimal) AS x, ST_GeomFromText(geometry) AS geometry FROM ...
. Otherwise they will all be of type string.hfs– hfs2022年09月13日 09:42:06 +00:00Commented Sep 13, 2022 at 9:42
http://www.gdal.org/drv_vrt.html plus http://www.gdal.org/drv_csv.html should have everything you need to describe the layout of your data and import it. In short, you need to create a OGR VRT file that describes things using details from the CSV driver page.