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
.
I think I figured it out...
After checking user30184 comment realized that I was sending petition to .../rest/layers/LayerName.xml
Instead of
...rest/layers/workspace:tasmania_cities.xml
Tried then with:
curl -v -u admin:geoserver -XDELETE "http://testdevt.fractanet.com.mx:181/geoserver/rest/layers/Workspace:layer.xml"
and the layer disappeared.
Then I tried to recreate the layer but geoserver told me that the layer was present on that datastore. Sent curl -v -u admin:geoserver -XDELETE "http://testdevt.fractanet.com.mx:181/geoserver/rest/workspaces/MyWorkspace/datastores/MyDatastore/featuretypes/Layername.xml"
and now I can recreate the layer
Best Answer
As user30184 said, the importer extension has been designed exactly for this job, and will convert the data into the format of the chosen target store (e.g., will import the data into postgis or oracle for you).
WFS-T is more geared towards single feature editing than "mass import" and can only handle GML anyways.