gicait / geoserver-rest

Python library for management of geospatial data in GeoServer.
https://geoserver-rest.readthedocs.io
MIT License
197 stars 79 forks source link

add publish_featurestore_sqlview #26

Closed fantomasdm closed 3 years ago

fantomasdm commented 3 years ago

I try to write a function for add a layer as sql view, but I get error "Error looking up primary key". This is code:

def publish_featurestore_sqlview(self, store_name, name, sql, keyColumn,geoName,geoType, workspace=None):
        try:
            if workspace is None:
                workspace = 'default'
            c = pycurl.Curl()
            layer_xml = """<featureType>
            <name>{0}</name>
            <enabled>true</enabled>
            <namespace>
            <name>{5}</name>
            </namespace>
            <title>{0}</title>
            <srs>EPSG:4326</srs>
            <metadata>
            <entry key="JDBC_VIRTUAL_TABLE"> 
            <virtualTable>
            <name>{0}</name>
            <sql>{1}</sql>
            <escapeSql>true</escapeSql>
            <keyColumn>{2}</keyColumn>
            <geometry>
            <name>{3}</name>
            <type>{4}</type>
            <srid>4326</srid>
            </geometry>
            </virtualTable>
            </entry>
            </metadata>
            </featureType>""".format(name,sql,keyColumn,geoName,geoType,workspace)
            c.setopt(pycurl.USERPWD, self.username + ':' + self.password)
            print (layer_xml)
            # connecting with the specified store in geoserver
            c.setopt(c.URL, '{0}/rest/workspaces/{1}/datastores/{2}/featuretypes'.format(
                self.service_url, workspace, store_name))
            c.setopt(pycurl.HTTPHEADER, ["Content-type: text/xml"])
            c.setopt(pycurl.POSTFIELDSIZE, len(layer_xml))
            c.setopt(pycurl.READFUNCTION, DataProvider(layer_xml).read_cb)
            c.setopt(pycurl.POST, 1)
            print("qui")
            c.perform()
            c.close()
            print("qui2")
        except Exception as e:
            return "Error:%s" % str(e)
fantomasdm commented 3 years ago

I have found mistake!

This new code:

def publish_featurestore_sqlview(self, store_name, name, sql, keyColumn,geoName,geoType,srs='32633', workspace=None):
        """
        Only user for postgis vector data
        input parameters: specify the query for view in the postgis database to be published, specify the store,workspace name,primary key column,geo column, geo type, srs and  the Geoserver user name, password and URL
        """
        try:
            if workspace is None:
                workspace = 'default'
            c = pycurl.Curl()
            layer_xml = """<featureType>
            <name>{0}</name>
            <enabled>true</enabled>
            <namespace>
            <name>{5}</name>
            </namespace>
            <title>{0}</title>
            <srs>EPSG:{7}</srs>
            <metadata>
            <entry key="JDBC_VIRTUAL_TABLE"> 
            <virtualTable>
            <name>{0}</name>
            <sql>{1}</sql>
            <escapeSql>false</escapeSql>
            <keyColumn>{2}</keyColumn>
            <geometry>
            <name>{3}</name>
            <type>{4}</type>
            <srid>{7}</srid>
            </geometry>
            </virtualTable>
            </entry>
            </metadata>
            <store class="dataStore">
            <name>{6}</name>
            </store>
            </featureType>""".format(name,sql,keyColumn,geoName,geoType,workspace,store_name,srs   )
            c.setopt(pycurl.USERPWD, self.username + ':' + self.password)
            print (layer_xml)
            # connecting with the specified store in geoserver
            c.setopt(c.URL, '{0}/rest/workspaces/{1}/datastores/{2}/featuretypes'.format(
                self.service_url, workspace, store_name))
            c.setopt(pycurl.HTTPHEADER, ["Content-type: text/xml"])
            c.setopt(pycurl.POSTFIELDSIZE, len(layer_xml))
            c.setopt(pycurl.READFUNCTION, DataProvider(layer_xml).read_cb)
            c.setopt(pycurl.POST, 1)
            print("qui")
            c.perform()
            c.close()
            print("qui2")
        except Exception as e:
            return "Error:%s" % str(e)

For using:

from geo.Geoserver import Geoserver
geo = Geoserver('http://localhost:8081/geoserver', username='admin', password='#')
geo.publish_featurestore_sqlview(workspace='pippo', store_name='DB_GioSviluppo',name="pySqlView",sql="(select id,nome,geometrie from \"GioSviluppo\".tabella)",keyColumn="id",geoName="geometrie",geoType="MultiPolygon")

schema, is between "

iamtekson commented 3 years ago

Hello @fantomasdm, Thank you for suggesting the new feature in this library. It looks really nice to have a new feature. I am glad you can solve this issue by yourself. It would be really nice if you can send me a pull request. Otherwise, I will add this feature by myself.

fantomasdm commented 3 years ago

Sorry...but I never pull request before... I'm afraid to do damage!

iamtekson commented 3 years ago

Then it is completely fine. I will implement this. Thank you very much for your contribution. I am really appreciated.