is there anyway to access Rest Api of Geoserver using python other than gsconfig.
since it doesn't support for me.i'm using python3.4
Like creating workspace,adding layers programmatically to Geoserver
[GIS] Accessing Rest Api of Geoserver using python
geoserverpython
Related Solutions
Which version of Geoserver are you using? To avoid the authentication just go to the "authentication" page in the security menu of the web-admin, select "REST" from the filter chain and check "no security" :)
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
.
Related Question
- [GIS] Python Geoserver gsconfig create resource from Postgis store table
- [GIS] Geoserver REST API: Duplicate layer name
- [GIS] How to configure a available feature types in GeoServer using python gsconfig
- [GIS] Updating bounding box of layer using geoserver API
- [GIS] Creating Workspace, Data Store and Layer via GeoServer REST API
Best Answer
As already mentioned slightly by Slslam, you can use Python's requests module to send the same POST requests to GeoServer's REST API as you could do from the command line with cUrl.
This is an example that I recently used to activate a layer not yet known to GeoServer but already available in the connected DataStore of the given workspace (a PostGIS database in my case), including activation of the TIME dimension.
In fact, the only thing that is different to the examples given on the cUrl page above is how to fire the POST request. Apart from that, the XML you send to the REST API can contain whatever you want as long as it is valid (otherwise, you'll get an error message back from the server in your requests variable).
To use the module, either install it via
pip install requests
or, preferably on Windows systems, by downloading this binary and installing it viapip install path\to\the\file.whl
.