Using the IWkb interface does a nice job at converting between an IGeometry and WKB. From a WKB you can use the Microsoft.SqlServer.Types library to convert a WKB to SqlGeometry then back to WKT.
IWkb wkb = geometry as (IWkb); //(Where geometry is an instance of IGeometry)
byte[] wkb_bytes = new byte[wkb.WkbSize];
int byte_count = wkb.WkbSize;
wkb.ExportToWkb(ref byte_count, out wkb_bytes[0]);
At this point you have the WKB stored in wkb_bytes. If you want to go the next step to SqlGeometry then to WKT:
SqlGeometry sqlGeom = SqlGeometry.STGeomFromWKB(new SqlBytes(wkb_bytes), srid);
string wkt = sqlGeom.ToString();
The pyshp module is a bit tricky to get the hang of, but really useful once you get it going. I've written a script that reads in a csv of the example data and writes out a shapefile with the data stored as attributes of the correct datatypes. The pyshp/xbase datatyping has always been tricky for me until I found this user guide for the xbase format and as a result of this question I have written a small note on my blog regarding the relevant pyshp datatypes, part of which I have pasted below:
- C is ASCII characters
- N is a double precision integer limited to around 18 characters in length
- D is for dates in the YYYYMMDD format, with no spaces or hyphens between the sections.
- F is for floating point numbers with the same length limits as N
- L is for logical data which is stored in the shapefile's attribute table as a short integer as a 1 (true) or a 0 (false). The values it can receive are 1, 0, y, n, Y, N, T, F or the python builtins True and False
The full listing is as follows:
import shapefile as shp
import csv
out_file = 'GPS_Pts.shp'
#Set up blank lists for data
x,y,id_no,date,target=[],[],[],[],[]
#read data from csv file and store in lists
with open('input.csv', 'rb') as csvfile:
r = csv.reader(csvfile, delimiter=';')
for i,row in enumerate(r):
if i > 0: #skip header
x.append(float(row[3]))
y.append(float(row[4]))
id_no.append(row[0])
date.append(''.join(row[1].split('-')))#formats the date correctly
target.append(row[2])
#Set up shapefile writer and create empty fields
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('X','F',10,8)
w.field('Y','F',10,8)
w.field('Date','D')
w.field('Target','C',50)
w.field('ID','N')
#loop through the data and write the shapefile
for j,k in enumerate(x):
w.point(k,y[j]) #write the geometry
w.record(k,y[j],date[j], target[j], id_no[j]) #write the attributes
#Save shapefile
w.save(out_file)
I hope this helps.
Best Answer
Pyshp does not have a WKT method but it does support the geo_interface protocol at the shape level. That protocol returns each shape as geojson. You can then use the lightweight, pure-python pygeoif module to convert to WKT.
The pygeoif module is available on the Python Package Index and Github:
The following example converts a point shapefile to MultiPoint WKT. Note the shapefile is just a point shapefile, not MultiPoint, but it makes it easier to see the WKT output:
The geometry variable "g" contains the individual points as WKT. You could also use pyshp's shapeRecords() method combined with pygeoif's feature() method to collect the attributes in addition to the geometry and convert to a FeatureCollection. The examples on github for both libraries demonstrate these methods individually.