Hello,
I want to publish a polyline shapefile to geoserver.
Sometimes it works, sometimes it does not. I do not understand why.
Here is my code to publish:
from geoserver.catalog import Catalog

def save_geoserver(data):
    geoserver_uri = 'http://localhost:8080/geoserver'
    from geoserver.workspace import ResourceInfo
    cat = Catalog("http://localhost:8080/geoserver/rest";, username='admin',
password='geoserver')
    wks_name = 'newWorkspaceName'
    uri = geoserver_uri +"/"+ wks_name
    store_name = 'newStore'

    data = data

    def check_workspace(workspace_name):
        print("test for workspace")
        check_workspace = False #create a variable to test if workspace
exists already
        if(cat.get_workspaces(workspace_name)):
            print("found workspace")
            check_workspace = True #set the variable to true, if workspace
exists
        return check_workspace

    #check_workspace(wks_name)
    check_store = False

    # def create_data(store_name, data, data_name, wks_name):
    #     cat.create_featurestore(store_name, data, workspace=wks_name)
    #     cat.add_data_to_store(store=store_name, name=data_name,
data=data, workspace=wks_name)
    #     print(store_name.workspace)

    if check_workspace(wks_name): # if workspace exists, create the
featurestore
        # print("Workspace exists")
        if(cat.get_store(store_name, workspace=wks_name)):
            cat.add_data_to_store(store=store_name, name=store_name,
data=data, workspace=wks_name)
            print("Store found. Data has been written.")
        else:
            print("Store does not exist. Store has been created. Data has
been written to the store.")
            cat.create_featurestore(store_name, data, wks_name)
            #create_data(store_name, data, "Test name", wks_name)
            cat.add_data_to_store(store=store_name, name=store_name,
data=data, workspace=wks_name)

    else: #if workspace does not exists, create the workspace and the
featurestore
        cat.create_workspace(wks_name, uri)
        #create_data(store_name, data, "Test name", wks_name)
        cat.add_data_to_store(store=store_name, name=store_name, data=data,
workspace=wks_name)
        print("Workspace does not exist. Workspace and store have been
created. Data has been written to the store.")

    # try:
    #     cat.publish_featuretype(data, cat.get_store(store_name,
wks_name),"GCS_WGS_1984")
    # except Exception as e:
    #     print("Error: ", e)

save_geoserver(r'C:\Users\this-pc\Documents\Navoi\Navoi.zip')

I can see the files saved in geoserver, however I cannot access them via
WMS, in JOSM.
I can see one of the files via WMS in JOSM, so I was able to do it. Perhaps
I may have made a change, but now it does not work.
The zip file contains the shp, .prj, .cpg, .dbf, .qpj, .shx files.
I was able to publish it previously, when also I was using a geojson, that
I imported to QGIS, saved as shapefile, then created an archive.
One of the reason I think it does not work is that i made a script to
create the shapefile from a json file automatically and save it to a zip
file, without making the steps manually in QGIS. I used the pyshp library.
You can see the code below.

def write_shp(input, output):
    geometries = []
    with shapefile.Writer(output) as w:
        w.field('FID', 'C')
        for i in input['features']:
            try:
                geometries.append(i['geometry']['coordinates'])
                #print("geometry: ", i['geometry']['coordinates'])

            except Exception as error:
                print("Error appending line to geometries list: ", error)
        #print(len(geometries))
        try:
            for i in geometries:
                w.line(geometries)
                w.record(geometries.index(i)+1)
        except Exception as error:
            print("Error adding lines: ", error )
        w.close()
def createPrjFile(shpFile):
    try:
        prjFile = open(shpFile + '.prj', 'w')
        prjStr = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
        prjFile.write(prjStr)
        prjFile.close()
        cpgFile = open(shpFile + '.cpg', 'w')
        cpgStr = 'UTF-8'
        cpgFile.write(cpgStr)
        cpgFile.close()
        qpjFile = open(shpFile + '.qpj', 'w')
        qpjStr = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]'
        qpjFile.write(qpjStr)
        qpjFile.close()
        print("Additional files written succesfully.")
    except Exception as error:
        print("Error creating additional files: ", error)

def createZipFile(path):
    zip_name = str(path.rsplit('\\', 1)[1])
    print(zip_name)
    files_list = []
    for i in ['.dbf','.prj','.shx','.shp','.cpg','.qpj']:
        files_list.append(zip_name+str(i))
    #print(files_list)
    try:
        print("Creating a zip archive from shapefile and .dbf, .prj, .shx
files")
        zipObj = ZipFile(path+'.zip', 'w')
        for item in files_list:
            zipObj.write(path.rsplit('\\', 1)[0]+'\\' + item, arcname =
item)
        print("Shapefile has been archived to: ", path.strip() + ".zip")
        zipObj.close()
    except Exception as error:
        print("Error saving to zip file: ", error)

Any help would be great.
Regards

-- 
*_Grab is hiring. Learn more at _**https://grab.careers 
<https://grab.careers/>*


By communicating with Grab Inc and/or its 
subsidiaries, associate companies and jointly controlled entities (“Grab 
Group”), you are deemed to have consented to the processing of your 
personal data as set out in the Privacy Notice which can be viewed at 
https://grab.com/privacy/ <https://grab.com/privacy/>


This email contains 
confidential information and is only for the intended recipient(s). If you 
are not the intended recipient(s), please do not disseminate, distribute or 
copy this email Please notify Grab Group immediately if you have received 
this by mistake and delete this email from your system. Email transmission 
cannot be guaranteed to be secure or error-free as any information therein 
could be intercepted, corrupted, lost, destroyed, delayed or incomplete, or 
contain viruses. Grab Group do not accept liability for any errors or 
omissions in the contents of this email arises as a result of email 
transmission. All intellectual property rights in this email and 
attachments therein shall remain vested in Grab Group, unless otherwise 
provided by law.

_______________________________________________
Geoserver-users mailing list

Please make sure you read the following two resources before posting to this 
list:
- Earning your support instead of buying it, but Ian Turton: 
http://www.ianturton.com/talks/foss4g.html#/
- The GeoServer user list posting guidelines: 
http://geoserver.org/comm/userlist-guidelines.html

If you want to request a feature or an improvement, also see this: 
https://github.com/geoserver/geoserver/wiki/Successfully-requesting-and-integrating-new-features-and-improvements-in-GeoServer


Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users

Reply via email to