I've been able to achieve this using the code segments below. But it'll be great if someone can come up with a better solution.
Part I: Uploading a shape file to GeoServer without PostGIS
public bool UploadShapeFile(string workspace, string dsName, Uri zipUri)
{
String fileUri = zipUri.AbsolutePath;
Console.Write(fileUri);
byte[] localShapeFile = readLocalShapeFile(fileUri);
String sUrl = "http://localhost:8081/geoserver/rest/workspaces/" +
workspace + "/datastores/" +
dsName + "/file.shp";
WebRequest request = WebRequest.Create(sUrl);
request.ContentType = "application/zip";
request.Method = "PUT";
request.Credentials = new NetworkCredential("geoserver-username", "passwd");
Stream requestStream = request.GetRequestStream();
requestStream.Write(localShapeFile, 0, localShapeFile.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Console.Write("Response from GeoServer: " + response);
return false;
}
private byte[] readLocalShapeFile(string filePath)
{
byte[] buffer;
FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try {
int length = (int)fStream.Length;
buffer = new byte[length];
int count;
int sum = 0;
while ((count = fStream.Read(buffer, sum, length - sum)) > 0)
sum += count;
}
finally {
fStream.Close();
}
return buffer;
}
Part II: Upload Shape File to GeoServer's PostGIS store
First, create a DB Data Store as shown:
public string CreateDbDataStore(string ws, string dsName)
{
String gUrl = "http://localhost:8081/geoserver/rest/workspaces/" + ws + "/datastores.xml";
WebRequest request = WebRequest.Create(gUrl);
request.ContentType = "application/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("geoserver-username", "passwd");
string dbXml = getDbXml(dsName);
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(dbXml);
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
Console.Write("Response from GeoServer: " + response);
return dsName;
}
Then, create a DB Table and FeatureType as shown:
public bool CreatePostGISTableAndFeatureType(string ws, string ds, string title, string projection)
{
string featXml = GetFeatureXml(ds, title, projection);
string fUrl = "http://localhost:8081/geoserver/rest/workspaces/" + ws +
"/datastores/" + ds + "/featuretypes";
WebRequest request = WebRequest.Create(fUrl);
request.ContentType = "application/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("geoserver-username", "passwd");
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(featXml);
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();
WebResponse response = request.GetResponse();
return false;
}
private string GetFeatureXml(string dsName, string title, string projection)
{
string fXml = "<featureType>" +
"<name>" + dsName + "</name>" +
"<nativeName>" + dsName + "</nativeName>" +
"<title>" + title + "</title>" +
"<srs>" + projection + "</srs>" +
"<attributes>" +
"<attribute>" +
"<name>the_geom</name>" +
"<binding>com.vividsolutions.jts.geom.Point</binding>" +
"</attribute>" +
"<attribute>" +
"<name>description</name>" +
"<binding>java.lang.String</binding>" +
"</attribute>" +
"<attribute>" +
"<name>timestamp</name>" +
"<binding>java.util.Date</binding>" +
"</attribute>" +
"</attributes>" +
"</featureType>";
return fXml;
}
Finally, upload the shape file using the UploadShapeFile(..)
method above, specifying the previously created (postgis) data store as the dsName
.
A POST will always create a new resource and redirect you to it once created.
If you want to either create a new one or replace an existing one you must use PUT instead.
Mind, it will overwrite fully an existing layer group (btw, haven't tried it out, the above is just based on what you should expect from a REST based API).
Best Answer
I don't think you can do this using GeoServer - it isn't what the REST interface (or GeoServer) was designed to do. You need to use shp2psql (or any other uploader like ogr2ogr) to add shapefiles to PostGIS. The GeoServer REST interface is designed for adding new stores/layers etc to GeoServer.