4

I have a text file containing exactly this:

POLYGON ((6.2973716 48.98613 1772.1, ... many other Long Lat Altitude values ... ))

And only this. It seems to be just like a WKT geometry of a polygon. But it's only a text file.

I'd like to create an "ESRI Shapefile" from this file, if possible with tools that are available without any installation on Windows.

I've tried GDAL so far but it doesn't seem to handle such files.

  • re: "any installation on Windows" -- do you have Windows and you can't install anything? Or you don't have Windows OS? – Jay Cummins Sep 23 '16 at 19:08
  • It's on a Windows OS computer and I don't have any rights. – s.k Sep 23 '16 at 19:26
  • I'm not sure if/how the QGIS plugins work with portable versions of QGIS, but you shouldn't require any additional rights. Just a good chunk of disk space: PortableGIS 5.6 cotnaining QGIS 2.14.1 LTR is here: my.pcloud.com/publink/… – Jay Cummins Sep 23 '16 at 19:31
  • 1
    ogr is part of the gdal library in my mind. ;) – s.k Sep 23 '16 at 19:41
  • 2
6

If it's a one-off operation, you can install the QuickWKT plugin for QGIS.

Press the Black/Red WKT Button: enter image description here and enter your WKT:
enter image description here

And press OK. That will add the layer. Then right-click to save the vector layer choosing the ESRI Shapefile format and including the z-dimension:

enter image description here

enter image description here

  • Is this plugin based on specific libraries that may be used outside QGIS? – s.k Sep 23 '16 at 19:42
  • I have no idea--I'm an idiot when it comes to QGIS development! The source is here: github.com/elpaso/quickwkt/blob/master/QuickWKT.py and it looks like `QgsGeometry.fromWkt. Not sure if QGIS can be invoked via a python script or not (that's probably worthy of its own question). – Jay Cummins Sep 23 '16 at 19:50
  • 1
    You can import WKT from textfiles with the CSV layer add dialog as well, no need for the plugin. – bugmenot123 Sep 24 '16 at 7:54
  • 1
    You can also copy to clipboard and Edit -> Paste As Layer to make a new layer – Nathan W Sep 24 '16 at 12:49
  • @NathanW That's a pretty cool feature although I couldn't get it to work for every geometry type (QGIS Ver. 2.16.1). Points and Lines worked. For Polygons, i get this: i.imgur.com/WVexFhL.png. For MultiPolygons, no error but I get a layer with 1 feature with null geometry. When I paste, I get double CRS prompts. – Jay Cummins Sep 26 '16 at 12:03
3

You can use the builtin arcpy function: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/fromwkt.htm

Something like this will get you there:

fo = open(inFile,'r')
wktString = fo.readlines()
fo.close()
polyGeom = arcpy.FromWKT(wktString[0],sr)
arcpy.CopyFeatures_management(polyGeom,os.path.join(outFolder,outName))
2

OpenJUMP zip installation https://sourceforge.net/projects/jump-pilot/files/OpenJUMP/1.9.1/ is portable and does not require special rights but java must be installed on the computer. I fear that it would be difficult to call OpenJUMP from external scripts. For that purpose I recommend the GDAL VRT route.

With OpenJUMP all you need to do is to create a new layer and copy WKT data through a clipboard with Ctrl-C / Ctrl-V. However, you can do the same also with a special tool "Add New Features".

enter image description here

enter image description here

enter image description here

Finally save into shapefile with Save as...

2

Without any additional software, you can take the VRT solution from How can I convert a csv file of WKT data to a shape file using ogr2ogr?

Since your coordinates seem to be in degrees, the LayerSRS can be taken as WGS84. You may grab the content of the .prj file of any shapefile in that projection, and eliminate line breaks from it:

<OGRVRTDataSource>
  <OGRVRTLayer name="lineWKT">
   <SrcDataSource>F:\Data\lineWKT.csv</SrcDataSource>
   <GeometryType>wkbLineString25D</GeometryType>
   <LayerSRS>GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] </LayerSRS>
   <GeometryField encoding="WKT" field='gm' > </GeometryField >
  </OGRVRTLayer>
</OGRVRTDataSource>
  • Using ogr VRT truncated the input data to 254 characters in the output *.SHP. I have a polygon with thousands of points given in a WKT POLYGON in my text file... 254 char is far not enough! Is there any workaround? – s.k Oct 5 '16 at 19:01
2

I was able to use GDAL's Python bindings to perform the operation. You can use the following as a command-line script or modify for one-off operation. It will read in WKT strings, one per line, from a file and write them into a shapefile. Be sure to set the projection correctly.

#!/usr/bin/env python3

import csv
import ogr
import os
import osr
import sys

if len(sys.argv)!=3:
  print("Syntax: {0} <Input File> <Output File>".format(sys.argv[0]))
  sys.exit(-1)

input_file  = sys.argv[1]
output_file = sys.argv[2]
layer_name  = os.path.splitext(os.path.basename(output_file))[0]

spatialref = osr.SpatialReference()  # Set the spatial ref.
spatialref.ImportFromProj4('+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs')
#spatialref.SetWellKnownGeogCS('WGS84')  # WGS84 aka ESPG:4326

driver = ogr.GetDriverByName("ESRI Shapefile")
dstfile = driver.CreateDataSource(output_file) # Your output file

# Please note that it will fail if a file with the same name already exists
dstlayer = dstfile.CreateLayer(layer_name, spatialref, geom_type=ogr.wkbMultiPolygon) 

# Add the other attribute fields needed with the following schema :
fielddef = ogr.FieldDefn("ID", ogr.OFTInteger)
fielddef.SetWidth(10)
dstlayer.CreateField(fielddef)

# Read the feature in your csv file:
with open(input_file) as fin:
  for nb, row in enumerate(fin.readlines()): 
    # WKT is in the second field in my test file :
    poly = ogr.CreateGeometryFromWkt(row)
    feature = ogr.Feature(dstlayer.GetLayerDefn())
    feature.SetGeometry(poly)
    feature.SetField("ID", nb) # A field with an unique id.
    dstlayer.CreateFeature(feature)
    feature.Destroy()
dstfile.Destroy()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.