3

I have been writing some python code to automatically add data layers from PostGIS to GeoServer and attach a suitable SLD as default. I got this working using the following code:

store=("<dataStore><name>data</name><connectionParameters><host>localhost</host><port>5432</port><database>case</database><schema>public</schema><user>" + postgisusername + "</user><passwd>" + postgispassword + "</passwd><dbtype>postgis</dbtype></connectionParameters></dataStore>")

c = pycurl.Curl()
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTPHEADER, ['Content-type: text/xml'])
c.setopt(c.USERPWD, username + ":" + password)
c.setopt(c.URL, 'http://localhost:8080/geoserver/rest/workspaces/test/datastores')
c.setopt(c.CUSTOMREQUEST, "POST")
c.setopt(c.POSTFIELDS, store)
c.perform()
c.close()


sldname='<style><name>DataLine</name><filename>DataLine.sld</filename></style>'

c = pycurl.Curl()
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTPHEADER, ['Content-type: text/xml'])
c.setopt(c.USERPWD, username + ":" + password)
c.setopt(c.URL, 'http://localhost:8080/geoserver/rest/styles')
c.setopt(c.CUSTOMREQUEST, "POST")
c.setopt(c.POSTFIELDS, sldname)
c.perform()
c.close()

filesize = os.path.getsize("Data Line.sld")
f = open("Data Line.sld", 'rb')

c = pycurl.Curl()
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTPHEADER, ['Content-type: application/vnd.ogc.sld+xml'])
c.setopt(c.USERPWD, username + ":" + password)
c.setopt(c.INFILESIZE, filesize)
c.setopt(c.URL, 'http://localhost:8080/geoserver/rest/styles/DataLine')
c.setopt(c.PUT, 1)
c.setopt(c.INFILE, f)
c.perform()
c.close()
f.close()

sldtolayer='<layer><defaultStyle><name>DataLine</name></defaultStyle><enabled>true</enabled></layer>'

c = pycurl.Curl()
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTPHEADER, ['Content-type: text/xml'])
c.setopt(c.USERPWD, username + ":" + password) 
c.setopt(c.URL, 'http://localhost:8080/geoserver/rest/layers/dataline')
c.setopt(c.CUSTOMREQUEST, "PUT")
c.setopt(c.POSTFIELDS, sldtolayer)
c.perform()
c.close()

This works very well for all SLDs where I am not referencing an external graphic such as an SVG for symbology. I moved a folder of SVGs into the styles folder within the data directory and restarted the OpenGeo Suite, to make sure it knew the SVGs existed.

However, the code creates a blank SLD within GeoServer.

The error message I receive is a Java Unknown Source error and external graphic location.

I know this SLD works if I load through the GUI, so it must be something going on through the REST API.

Can anyone help?

thanks

  • Can you add the specific Java error and stack trace? – CHenderson Jan 20 '13 at 21:52
  • I've experienced the same problem and filed a bug report, jira.codehaus.org/browse/GEOS-6498 – Paul Ramsey May 26 '14 at 20:06
  • do you have relative or absolute paths in your SLD? – Ian Turton May 27 '14 at 7:29
  • This question is 5 years old and the GeoServer REST API has been rewritten fully in the meantime. Imho the question should be closed, but I don't see an option about it not being relevant anymore. What to do? – Andrea Aime Jul 3 '18 at 8:03

Your Answer

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

Browse other questions tagged or ask your own question.