I am using gsconfig and python in order to retrieve information regarding some geoserver layers.

What I am trying to do is to find out if a specific layer is raster or vector. I read online that I need to use get_resource function to do that.

This is what I do:

name = "layer_name"
workspace = "my_workspace"
cat = Catalog(location, user,pass)
resource = cat.get_resource(name, workspace=workspace)

The question is how to get the type of the layer after that. When I print the resource I get:

 <geoserver.resource.Coverage object at 0x7fb2f8ffca90>

And if the layer is vector then I get:

 <geoserver.resource.FeatureType object at 0x7fb2f9e64c50>

What's the way to access the properties of the layer and see if its vector or raster so later I can do something like this:

if layer = "raster":
   # do something
elif layer = "vector":
  # do something else


 print dir(resource)

I get back a list with the attributes of the class:

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_href', '_store', '_workspace', 'abstract', 'advertised', 'catalog', 'clear', 'dirty', 'dom', 'enabled', 'fetch', 'href', 'keywords', 'latlon_bbox', 'message', 'metadata', 'metadata_links', 'name', 'native_bbox', 'projection', 'projection_policy', 'refresh', 'request_srs_list', 'resource_type', 'response_srs_list', 'save_method', 'serialize', 'store', 'supported_formats', 'title', 'url_part_stores', 'url_part_types', 'workspace', 'writers']

With help from @iant, I found a way to see if my layer is vector or raster. I am not sure if this is the best way to do this but it works.

I actually followed step by step @iant's answer but in my if statements instead of putting:

if type(resource) is geoserver.resource.Coverage:
    # this wouldn't work properly.

I put this:

a = str(type(resource)) 
if a == "<class 'geoserver.resource.Coverage'>": 

So I actually transformed the result of type(resource) into a string. It should be the case that the first way does work. I am not sure why it fails.

  • 1
    does stackoverflow.com/questions/2225038/… help? – Ian Turton Jun 21 '16 at 13:36
  • Using dir(resource) I get back a list of the attributes of the class as I added on my edit. I am not sure if some of this attribute indicates the layer's type! – user1919 Jun 21 '16 at 13:47
  • I was thinking of type(layer) is Coverage – Ian Turton Jun 21 '16 at 14:00
  • I am not getting you. type(resource) will return geoserver.resource.Coverage or geoserver.resource.FeatureType. – user1919 Jun 21 '16 at 14:17

Based on my reading of this question you should be able to do:

name = "layer_name"
workspace = "my_workspace"
cat = Catalog(location, user,pass)
resource = cat.get_resource(name, workspace=workspace)

if type(resource) is geoserver.resource.Coverage:
elif type(resource) is geoserver.resource.FeatureType:

Or possibly:

if isinstance(resource, geoserver.resource.Coverage):
  • Thanks for the answer. I try this but it returns an error. When I print the type(resource) I get: <class 'geoserver.resource.FeatureType'>. Should I try comparing type(resource) is class 'geoserver.resource.FeatureType' ? – user1919 Jun 21 '16 at 14:33
  • I don't know if this is good practice but I decided to transform the type(resource) into a string and compare in your if statements the two strings as: a = str(type(resource)) if a == "<class 'geoserver.resource.Coverage'>": ... – user1919 Jun 21 '16 at 14:37
  • Best practice is to use isinstance. – alphabetasoup Jun 21 '16 at 23:54
  • Yup. But none of these work. When I try to use the isinstance my atom pops up that geoserver is an unknown variable. I also tried to use quotes but this doesn't solve the problem. – user1919 Jun 22 '16 at 6:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.