<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Creating Geopackage Layers
Landung Setiawan 5/27/2016
Updated 6/29/2016
Note
Step1:  Reading csv and printing as dictionary
Step2:  Use shapely to make points
Since csv module doesn't distinguish between types, shapely is used to make points
Step3:  Geopandas reading a geopackage
Step4:  Write geopandas dataframe to geopackage
Step5:  Uploading Geopackage to GeoServer
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
# Import the necessary libraries
import csv, os
from shapely.geometry import Point, mapping
import fiona, shapely
from fiona import Collection
import numpy as np
print "fiona version: {}".format(fiona.__version__)
print "shapely version: {}".format(shapely.__version__)
print "gdal version: {}".format(fiona.__gdal_version__)
print "numpy version: {}".format(np.__version__)
# Assign file_path
pth = "/mnt/hgfs/shared_ubuntu/APL/OOI/OOI_ipynb/"
fname = 'Nanoos.gpkg'
fcsv = "OOI_Assets.csv"
Explanation: Creating Geopackage Layers
Landung Setiawan 5/27/2016
Updated 6/29/2016
Note: In order for fiona to be able to read and write geopackage, numpy 1.10.0 and gdal 1.11.0 or greater is required, however, gdal cannot be 2.0.0 or greater!
Creating the environment
bash
conda create -n gpkg -c conda-forge numpy=1.10.0 fiona=1.6.4 gdal=1.11.4 geopandas matplotlib
source activate gpkg
conda install ipython notebook anaconda-client
conda install -c auto gsconfig=0.6.7
End of explanation
with open(os.path.join(pth,fcsv),'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print row # Notice that numbers are strings in this case
Explanation: Reading csv and printing as dictionary
End of explanation
with open(os.path.join(pth,fcsv), 'rb') as f:
    reader = csv.DictReader(f)
    for row in reader:
        point = Point(float(row['Lon']),float(row['Lat']))
        print point
Explanation: Use shapely to make points
Since csv module doesn't distinguish between types, shapely is used to make points
End of explanation
import pandas as pd
from geopandas import GeoDataFrame
from shapely.geometry import Point
import matplotlib.pyplot as plt
import geopandas as gpd
import pyproj
print "geopandas version: {}".format(gpd.__version__)
# Test reading geopackage
geopackage = gpd.read_file(os.path.join(pth,fname))
geopackage.head(2)
Explanation: Geopandas reading a geopackage
End of explanation
df = pd.read_csv(os.path.join(pth,fcsv))
# Assign CRS, retrieved from epsg.io, the example below is EPSG:4326
crs = '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"]]'
geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]
geo_df = GeoDataFrame(df, crs=crs, geometry=geometry)
print "Original Column Header: {}\n".format(geo_df.columns.values)
# Renamed the problematic keys
renamed = geo_df.rename(columns={'Provider URL':'Provider_URL',
                                 'Provider':'Provider',
                                 'Provider Type':'Provider_Type',
                                 'State / Province':'State_or_Province'})
print "Renamed Column Header: {}".format(renamed.columns.values)
# Removing the problematic keys
# Problematic keys can either be renamed or removed.
# package = geo_df.drop(geo_df.columns[[8,9,10,11]],axis=1)
# print package.columns.values 
# Write the renamed geodataframe to a geopackage
renamed.to_file('OOI_Assets.gpkg',driver='GPKG')
# Check if the geopackage was written correctly
test = gpd.read_file('OOI_Assets.gpkg')
test
Explanation: Write geopandas dataframe to geopackage
End of explanation
# Import the Catalog module
from geoserver.catalog import Catalog
# Import subprocess to use cURL REST API since gsconfig, doesn't seem to have this capability anymore
import subprocess
# Retrieve catalog from Geoserver Instance via REST (REpresentational State Transfer)
cat = Catalog("http://data.nanoos.org/geoserver2_8/rest", username='####', password='####')
# Get list of workspaces
print cat.get_workspaces()
# Get workspace
nvs = cat.get_workspace('nvs_assets')
print nvs.name
# Create the geopackage datastore
gpkg_ds = cat.create_datastore('OOI_Assets', workspace=nvs)
# Edit the connection parameters
gpkg_ds.connection_parameters = {'Connection timeout': '20',
 'Evictor run periodicity': '300',
 'Evictor tests per run': '3',
 'Expose primary keys': 'false',
 'Max connection idle time': '300',
 'Test while idle': 'true',
 'database': 'file:data/geopackages/OOI_Assets.gpkg', # Point to location of geopackage relative to the geoserver data directory
 'dbtype': 'geopkg',
 'fetch size': '1000',
 'max connections': '10',
 'min connections': '1',
 'namespace': 'http://data.nanoos.org/geoserver2_8/nvs_assets', # Workspace URL
 'validate connections': 'true'}
# Save datastore
cat.save(gpkg_ds)
# Set necessary variables for cURL
data_name = 'OOI_Assets'
wksp_name = nvs.name
ds_name = gpkg_ds.name
print ds_name
# Create layer from geopackage table
subprocess.call('curl -v -u ####:#### -XPOST -H "Content-type: text/xml" -d "<featureType><name>{0}</name></featureType>" http://data.nanoos.org/geoserver2_8/rest/workspaces/{1}/datastores/{2}/featuretypes'.format(data_name,wksp_name,ds_name), shell=True)
# get the newly published layer w/o any projection
layer = cat.get_layer(data_name)
# retrieve resource to assign projection
rsrc = layer.resource
# assign Layer projection
rsrc.projection = 'EPSG:4326'
# save layer
cat.save(rsrc)
Explanation: Uploading Geopackage to GeoServer
End of explanation
<END_TASK>