예제 #1
0
def upload_shapefile(id):

    '''
    Check to see if shapefile is on geoserver. If not, upload it.
    '''
    geoserver_engine = get_spatial_dataset_engine(name='default')
    response = geoserver_engine.get_layer(id, debug=True)
    if response['success'] == False:
        print('Shapefile was not found on geoserver. Uploading it now from app workspace')

        #Create the workspace if it does not already exist
        response = geoserver_engine.list_workspaces()
        if response['success']:
            workspaces = response['result']
            if WORKSPACE not in workspaces:
                geoserver_engine.create_workspace(workspace_id=WORKSPACE, uri=GEOSERVER_URI)

        #Create a string with the path to the zip archive
        project_directory = os.path.dirname(__file__)
        app_workspace = os.path.join(project_directory, 'workspaces', 'app_workspace')
        zip_archive = os.path.join(app_workspace, 'spatial_files', 'shapefiles', id + '.zip')

        # Upload shapefile to the workspaces
        store = id
        store_id = WORKSPACE + ':' + store
        geoserver_engine.create_shapefile_resource(
            store_id=store_id,
            shapefile_zip=zip_archive,
            overwrite=True
        )
예제 #2
0
def global_map(request):

    color_bar = get_color_bar()
    color_bar = json.dumps(color_bar)
    # Connecting to the Geoserver
    geoserver_engine = get_spatial_dataset_engine(name='default')
    stores = geoserver_engine.list_stores(workspace='globalgrace')

    grace_layer_options = []
    sorted_stores = sorted(stores['result'],
                           key=lambda x: datetime.strptime(x, '%Y_%m_%d'))
    for store in sorted_stores:
        year = int(store.split('_')[0])
        month = int(store.split('_')[1])
        day = int(store.split('_')[2])
        date_str = datetime(year, month, day)
        date_str = date_str.strftime("%Y %B %d")
        grace_layer_options.append([date_str, store])

    slider_max = len(grace_layer_options)

    select_layer = SelectInput(
        display_text='Select a day',
        name='select_layer',
        multiple=False,
        options=grace_layer_options,
    )

    context = {
        'select_layer': select_layer,
        'slider_max': slider_max,
        "color_bar": color_bar
    }

    return render(request, 'grace/global_map.html', context)
def home(request):
    """
    Controller for the app home page.

    geoserver_engine = get_spatial_dataset_engine(name=)
    """
    geoserver_engine = get_spatial_dataset_engine(name="default")
    response = geoserver_engine.list_workspaces()

    if response["success"]:
        workspaces = response["result"]
        if WORKSPACE not in workspaces:
            geoserver_engine.create_workspace(workspace_id=WORKSPACE, uri=GEOSERVER_URI)

    if request.POST and "submit" in request.POST:
        if request.FILES and "files" in request.FILES:
            file_list = request.FILES.getlist("files")
            store = "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(6))
            store_id = WORKSPACE + ":" + store
            response = geoserver_engine.create_shapefile_resource(
                store_id=store_id, shapefile_upload=file_list, overwrite=True, debug=True
            )

    context = {}

    return render(request, "second_app/home.html", context)
예제 #4
0
def clip_raster(watershed, uniqueID, outletID, raster_type):
    input_json = os.path.join(temp_workspace, uniqueID,
                              'basin_upstream_' + outletID + '.json')
    input_tif = os.path.join(data_path, watershed, 'Land',
                             raster_type + '.tif')
    output_tif = os.path.join(
        temp_workspace, uniqueID,
        watershed + '_upstream_' + raster_type + '_' + outletID + '.tif')

    subprocess.call(
        '/home/ubuntu/tethys/miniconda/envs/tethys/bin/gdalwarp --config GDALWARP_IGNORE_BAD_CUTLINE YES -cutline {0} -crop_to_cutline -dstalpha {1} {2}'
        .format(input_json, input_tif, output_tif),
        shell=True)

    storename = watershed + '_upstream_' + raster_type + '_' + outletID
    headers = {
        'Content-type': 'image/tiff',
    }
    user = geoserver['user']
    password = geoserver['password']
    data = open(output_tif, 'rb').read()

    geoserver_engine = get_spatial_dataset_engine(name='ADPC')
    response = geoserver_engine.get_layer(storename, debug=True)
    if response['success'] == False:
        request_url = '{0}workspaces/{1}/coveragestores/{2}/file.geotiff'.format(
            geoserver['rest_url'], geoserver['workspace'], storename)

        requests.put(request_url,
                     verify=False,
                     headers=headers,
                     data=data,
                     auth=(user, password))
    else:
        print('layer already exists')
예제 #5
0
def init_hydroshare_gis_layers_db(first_time):
    Base.metadata.create_all(engine)

    if first_time:
        spatial_dataset_engine = get_spatial_dataset_engine(name='default')
        spatial_dataset_engine.delete_workspace(workspace_id=get_workspace(),
                                                purge=True,
                                                recurse=True)
예제 #6
0
def init_hydroshare_gis_layers_db(engine, first_time):
    Base.metadata.create_all(engine)

    if first_time:
        spatial_dataset_engine = get_spatial_dataset_engine(
            name='NONE')  ### make name not hardcoded
        spatial_dataset_engine.delete_workspace(workspace_id=get_workspace(),
                                                purge=True,
                                                recurse=True)
def map(request):
    """
    Controller for the map page
    """
    geoserver_engine = get_spatial_dataset_engine(name='default')

    options = []

    response = geoserver_engine.list_layers(with_properties=False)

    if response['success']:
        for layer in response['result']:
            options.append((layer.title(), layer))

    select_options = SelectInput(display_text='Choose Layer',
                                 name='layer',
                                 multiple=False,
                                 options=options)

    map_layers = []

    if request.POST and 'layer' in request.POST:
        selected_layer = request.POST['layer']
        legend_title = selected_layer.title()

        geoserver_layer = MVLayer(
            source='ImageWMS',
            options={'url': 'http://localhost:8181/geoserver/wms',
                   'params': {'LAYERS': selected_layer},
                   'serverType': 'geoserver'},
            legend_title=legend_title,
            legend_extent=[-114, 36.5, -109, 42.5],
            legend_classes=[
                MVLegendClass('polygon', 'County', fill='#999999'),
        ])

        map_layers.append(geoserver_layer)


    view_options = MVView(
        projection='EPSG:4326',
        center=[-100, 40],
        zoom=4,
        maxZoom=18,
        minZoom=2
    )

    map_options = MapView(height='500px',
                          width='100%',
                          layers=map_layers,
                          legend=True,
                          view=view_options)

    context = {'map_options': map_options,
               'select_options': select_options}

    return render(request, 'geoserver_app/map.html', context)
def map(request):
    """controller for the map page"""
    # Define GeoServer Layer

    geoserver_engine = get_spatial_dataset_engine(name="default")

    options = []

    response = geoserver_engine.list_layers(with_properties=True)

    if response["success"]:
        for layer in response["result"]:
            resource_name = layer["resource"]

            if WORKSPACE in resource_name:
                options.append((resource_name, resource_name))

    select_options = SelectInput(display_text="Choose layer", name="layer", multiple=False, options=options)

    map_layers = []

    if request.POST and "layer" in request.POST and "deleteStore" not in request.POST:
        selected_layer = request.POST["layer"]
        parts = selected_layer.split(":")
        legend_title = parts[1]

        geoserver_layer = MVLayer(
            source="ImageWMS",
            options={
                "url": "http://127.0.0.1:8181/geoserver/wms",
                "params": {"LAYERS": selected_layer},
                "serverType": "geoserver",
            },
            legend_title=legend_title,
            legend_extent=[-120, 37, -100, 45],
        )
        map_layers.append(geoserver_layer)

    if request.POST and "deleteStore" in request.POST:
        selected_layer = request.POST["layer"]
        parts = selected_layer.split(":")
        legend_title = parts[1]
        store_id = WORKSPACE + ":" + parts[1]
        geoserver_engine.delete_layer(store_id)
        options.remove((store_id, store_id))
        select_options = SelectInput(display_text="Choose layer", name="layer", multiple=False, options=options)

    # this is a comment for testing "push" with git
    view_options = MVView(projection="EPSG:4326", center=[-120, 37], zoom=5, maxZoom=18, minZoom=2)
    map_options = MapView(height="400px", width="100%", layers=map_layers, view=view_options, legend=True)

    context = {"map_options": map_options, "select_options": select_options}
    return render(request, "second_app/map.html", context)
예제 #9
0
파일: model.py 프로젝트: SERVIR/SWAT2.0
def clip_raster(watershed, uniqueID, outletID, raster_type):
    logging.info("clip raster entered")
    input_json = os.path.join(temp_workspace, uniqueID,
                              'basin_upstream_' + outletID + '.json')
    input_tif = os.path.join(data_path, watershed, 'Land',
                             raster_type + '.tif')
    output_tif = os.path.join(
        temp_workspace, uniqueID,
        watershed + '_upstream_' + raster_type + '_' + outletID + '.tif')
    logging.info("clip raster before gdal")
    p = subprocess.Popen(
        '{0} --config GDALWARP_IGNORE_BAD_CUTLINE YES -cutline {1} -crop_to_cutline -dstalpha {2} {3}'
        .format(cfg.gdalwarp_path, input_json, input_tif, output_tif),
        shell=True)
    p.wait()
    logging.info("clip raster after gdal")

    storename = watershed + '_upstream_' + raster_type + '_' + outletID
    logging.info(storename)

    headers = {
        'Content-type': 'image/tiff',
    }
    user = cfg.geoserver['user']
    password = cfg.geoserver['password']
    logging.info("before reading tif")

    data = open(output_tif, 'rb').read()

    logging.info("after reading tif")

    geoserver_engine = get_spatial_dataset_engine(name='SCO')
    logging.info("after geoserver engine")

    response = geoserver_engine.get_layer(storename, debug=False)
    logging.info("after reading geoserver layer")

    if response['success'] == False:
        request_url = '{0}workspaces/{1}/coveragestores/{2}/file.geotiff'.format(
            geoserver['rest_url'], geoserver['workspace'], storename)
        logging.info("response success")

        requests.put(request_url,
                     verify=False,
                     headers=headers,
                     data=data,
                     auth=(user, password))
        logging.info("put into server")
예제 #10
0
def save_files(id):

    app_workspace = app.get_app_workspace()
    rch_path = os.path.join(app_workspace.path, 'rch_data', id)
    temp_path = temp_workspace
    temp_files = os.listdir(temp_path)

    for file in temp_files:
        if file.endswith('.rch'):
            print('saving file to app workspace')
            temp_file_path = os.path.join(temp_path, file)
            perm_file_path = os.path.join(rch_path, file)
            copyfile(temp_file_path, perm_file_path)
            os.remove(temp_file_path)
        elif file.endswith('.zip'):
            print('uploading file to geoserver')
            temp_file_path = os.path.join(temp_path, file)
            '''
            Check to see if shapefile is on geoserver. If not, upload it.
            '''
            geoserver_engine = get_spatial_dataset_engine(name='default')
            response = geoserver_engine.get_layer(file, debug=True)
            if response['success'] == False:

                #Create the workspace if it does not already exist
                response = geoserver_engine.list_workspaces()
                if response['success']:
                    workspaces = response['result']
                    if WORKSPACE not in workspaces:
                        geoserver_engine.create_workspace(
                            workspace_id=WORKSPACE, uri=GEOSERVER_URI)

                #Create a string with the path to the zip archive
                zip_archive = temp_file_path

                # Upload shapefile to the workspaces
                if 'reach' in file or 'drainageline' in file or 'stream' in file or 'river' in file:
                    store = id + '-reach'
                elif 'subbasin' in file or 'catch' in file or 'boundary' in file:
                    store = id + '-subbasin'
                print(store)
                store_id = WORKSPACE + ':' + store
                print(store_id)
                geoserver_engine.create_shapefile_resource(
                    store_id=store_id,
                    shapefile_zip=zip_archive,
                    overwrite=True)
            os.remove(temp_file_path)
예제 #11
0
def upload_shapefile(id, shp_path):
    """
    Check to see if shapefile is on geoserver. If not, upload it.
    """

    # Create a string with the path to the zip archive
    zip_archive = os.path.join(data_path, 'temp', 'shapefiles', id + '.zip')
    zip_ref = zipfile.ZipFile(zip_archive, 'r')
    zip_ref.extractall(shp_path)
    zip_ref.close()

    prj_path = os.path.join(shp_path, id + '.prj')
    f = open(prj_path)
    validate = 0
    for line in f:
        if 'PROJCS' in line:
            validate = 1
            print(
                'This shapefile is in a projected coordinate system. You must use a geographic coordinate system'
            )

    if validate == 0:
        geoserver_engine = get_spatial_dataset_engine(name='ADPC')
        response = geoserver_engine.get_layer(id, debug=True)

        WORKSPACE = geoserver['workspace']
        GEOSERVER_URI = geoserver['URI']

        if response['success'] == False:
            print(
                'Shapefile was not found on geoserver. Uploading it now from app workspace'
            )

            # Create the workspace if it does not already exist
            response = geoserver_engine.list_workspaces()
            if response['success']:
                workspaces = response['result']
                if WORKSPACE not in workspaces:
                    geoserver_engine.create_workspace(workspace_id=WORKSPACE,
                                                      uri=GEOSERVER_URI)

            # Upload shapefile to the workspaces
            store = id
            store_id = WORKSPACE + ':' + store
            geoserver_engine.create_shapefile_resource(
                store_id=store_id, shapefile_zip=zip_archive, overwrite=True)
    os.remove(zip_archive)
예제 #12
0
 def upload_layer_to_geoserver(self):
     # Geoserver parameters
     geo_eng = get_spatial_dataset_engine(name='default')
     # Create raster in geoserver
     response = geo_eng.create_coverage_resource(store_id=self.store_id,
                                                 coverage_file=self.zip_path,
                                                 coverage_type='worldimage',
                                                 overwrite=True,
                                                 )
     if not response['success']:
         result = geo_eng.create_workspace(workspace_id=get_workspace(),
                                           uri='tethys_app-%s' % get_workspace())
         if result['success']:
             self.upload_layer_to_geoserver()
     else:
         self.geoserver_url = geo_eng.endpoint.replace('rest', 'wms')
         self.loaded = True
예제 #13
0
def upload_dem(id, dem_path):
    """
    upload dem to user workspace and geoserver
    """

    shutil.copy2(os.path.join(data_path, 'temp', 'DEMfiles', id), dem_path)

    geoserver_engine = get_spatial_dataset_engine(name='ADPC')
    response = geoserver_engine.get_layer(id, debug=True)

    WORKSPACE = geoserver['workspace']
    GEOSERVER_URI = geoserver['URI']

    if response['success'] == False:
        print(
            'DEM was not found on geoserver. Uploading it now from app workspace'
        )

        # Create the workspace if it does not already exist
        response = geoserver_engine.list_workspaces()
        if response['success']:
            workspaces = response['result']
            if WORKSPACE not in workspaces:
                geoserver_engine.create_workspace(workspace_id=WORKSPACE,
                                                  uri=GEOSERVER_URI)

        file_path = os.path.join(dem_path, id)
        storename = id
        headers = {
            'Content-type': 'image/tiff',
        }
        user = geoserver['user']
        password = geoserver['password']
        data = open(file_path, 'rb').read()

        request_url = '{0}workspaces/{1}/coveragestores/{2}/file.geotiff'.format(
            geoserver['rest_url'], WORKSPACE, storename)

        requests.put(request_url,
                     verify=False,
                     headers=headers,
                     data=data,
                     auth=(user, password))
    os.remove(os.path.join(data_path, 'temp', 'DEMfiles', id))
def home(request):
    """
    Controller for the app home page.
    """
    # Retrieve a geoserver engine
    geoserver_engine = get_spatial_dataset_engine(name='default')

    # Check for workspace and create workspace for app if it doesn't exist
    response = geoserver_engine.list_workspaces()

    if response['success']:
        workspaces = response['result']

        if WORKSPACE not in workspaces:
            response = geoserver_engine.create_workspace(workspace_id=WORKSPACE, 
                                                         uri=GEOSERVER_URI)

    # Case where the form has been submitted
    if request.POST and 'submit' in request.POST:
        # Verify files are included with the form
        if request.FILES and 'files' in request.FILES:
            # Get a list of the files
            file_list = request.FILES.getlist('files')

            # Upload shapefile
            store = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(6))
            store_id = WORKSPACE + ':' + store
            response = geoserver_engine.create_shapefile_resource(
                        store_id=store_id,
                        shapefile_upload=file_list,
                        overwrite=True,
                        debug=True
            )

    context = {}

    return render(request, 'geoserver_app/home.html', context)
예제 #15
0
def home_graph(request, id):
    """
    Controller for home page to display a graph and map.
    """

    # Set up the map options
    '''
    First query geoserver to get the layer corresponding to id, then parse
    one of the URLs in the response DICT to get the bounding box.
    Then parse the box to get lat/long to properly center the map
    '''
    geoserver_engine = get_spatial_dataset_engine(name='default')
    response = geoserver_engine.get_layer(id, debug=False)
    kmlurl = response['result']['wms']['kml']
    parsedkml = urlparse.urlparse(kmlurl)
    bbox = urlparse.parse_qs(parsedkml.query)['bbox'][0]
    bboxitems = bbox.split(",")
    box_left = float(bboxitems[0])
    box_right = float(bboxitems[2])
    box_top = float(bboxitems[3])
    box_bottom = float(bboxitems[1])
    centerlat = (box_left + box_right) / 2
    centerlong = (box_top + box_bottom) / 2

    map_layers = []
    geoserver_layer = MVLayer(
        source='ImageWMS',
        options={
            'url': 'http://localhost:8181/geoserver/wms',
            'params': {
                'LAYERS': id
            },
            'serverType': 'geoserver'
        },
        legend_title=id,
        legend_extent=[box_left, box_bottom, box_right, box_top],
        legend_classes=[
            MVLegendClass('polygon', 'Boundary', fill='#999999'),
        ])

    map_layers.append(geoserver_layer)

    view_options = MVView(
        projection='EPSG:4326',
        center=[centerlat, centerlong],
        zoom=4,
        maxZoom=18,
        minZoom=2,
    )

    map_options = MapView(height='300px',
                          width='100%',
                          layers=map_layers,
                          legend=True,
                          view=view_options)

    # Set up the graph options
    project_directory = os.path.dirname(__file__)
    app_workspace = os.path.join(project_directory, 'workspaces',
                                 'app_workspace')
    csv_file = os.path.join(app_workspace, 'output', id, 'hydrograph.csv')
    with open(csv_file, 'rb') as f:
        reader = csv.reader(f)
        csvlist = list(reader)
    volume_time_series = []
    formatter_string = "%m/%d/%Y"
    for item in csvlist:
        mydate = datetime.strptime(item[0], formatter_string)
        volume_time_series.append([mydate, float(item[1])])

    # Configure the time series Plot View
    grace_plot = TimeSeries(engine='highcharts',
                            title=id + ' GRACE Data',
                            y_axis_title='Volume',
                            y_axis_units='cm',
                            series=[
                                {
                                    'name': 'Change in Volume',
                                    'color': '#0066ff',
                                    'data': volume_time_series,
                                },
                            ],
                            width='100%',
                            height='300px')

    context = {
        'map_options': map_options,
        'grace_plot': grace_plot,
        'reg_id': id
    }

    return render(request, 'grace_basic/home.html', context)
예제 #16
0
def home_graph(request, id):
    """
    Controller for home page to display a graph and map.
    """
    """
    SET UP THE MAP OPTIONS
    """
    '''
    QUERY GEOSERVER TO GET THE LAYER CORRESPONDING TO ID
    THEN PARSE ONE OF THE URLS IN THE RESPONSE DICT TO GET
    THE BOUNDING BOX, THEN PARSE THE BOX TO GET LAT LONG TO
    PROPERLY CENTER THE MAP
    '''
    geoserver_engine = get_spatial_dataset_engine(name='default')
    response = geoserver_engine.get_layer(id, debug=False)
    kmlurl = response['result']['wms']['kml']
    parsedkml = urlparse.urlparse(kmlurl)
    bbox = urlparse.parse_qs(parsedkml.query)['bbox'][0]
    bboxitems = bbox.split(",")
    box_left = float(bboxitems[0])
    box_right = float(bboxitems[2])
    box_top = float(bboxitems[3])
    box_bottom = float(bboxitems[1])

    centerlat = (box_left + box_right) / 2
    centerlong = (box_top + box_bottom) / 2

    map_layers = []

    geoserver_layer = MVLayer(
        source='ImageWMS',
        options={
            'url': 'http://localhost:8181/geoserver/wms',
            'params': {
                'LAYERS': id
            },
            'serverType': 'geoserver'
        },
        legend_title=id,
        legend_extent=[box_left, box_bottom, box_right, box_top],
        legend_classes=[
            MVLegendClass('polygon', 'Boundary', fill='#999999'),
        ])

    map_layers.append(geoserver_layer)

    view_options = MVView(
        projection='EPSG:4326',
        center=[centerlat, centerlong],
        zoom=4,
        maxZoom=18,
        minZoom=2,
    )

    map_options = MapView(
        height='300px',
        width='100%',
        layers=map_layers,
        legend=True,
        view=view_options,
        #basemap='MapQuest'
    )
    """
    SET UP THE GRAPH OPTIONS
    """
    project_directory = os.path.dirname(__file__)
    user_workspace = os.path.join(project_directory, 'workspaces',
                                  'user_workspaces')

    if not os.path.exists(user_workspace):
        os.makedirs(user_workspace)

    csv_file = os.path.join(user_workspace, 'output/' + id + '/hydrograph.csv')

    with open(csv_file, 'rb') as f:
        reader = csv.reader(f)
        csvlist = list(reader)

    volume_time_series = []
    formatter_string = "%m/%d/%Y"
    for item in csvlist:
        mydate = datetime.strptime(item[0], formatter_string)
        volume_time_series.append([mydate, float(item[1])])

    # Configure the time series Plot View
    grace_plot = TimeSeries(engine='highcharts',
                            title=id + ' GRACE Data',
                            y_axis_title='Volume',
                            y_axis_units='cm',
                            series=[
                                {
                                    'name': 'Change in Volume',
                                    'color': '#0066ff',
                                    'data': volume_time_series,
                                },
                            ],
                            width='100%',
                            height='300px')

    context = {
        'map_options': map_options,
        'grace_plot': grace_plot,
        'reg_id': id
    }

    return render(request, 'grace/home.html', context)
예제 #17
0
def load_tiff_ly(post_params):
    """
    This function returns the previously loaded map or the new map layer
    if the button on the page was clicked
    """
    map_layers = None

    if post_params.get('plotTime'):
        plot_time = post_params['plotTime']
    else:
        plot_time = None

    if post_params.get('model'):
        model = post_params['model']
    else:
        model = None

    if post_params.get('variable'):
        variable = post_params['variable']
    else:
        variable = None

    if model and variable and plot_time:
        # Geoserver parameters
        geo_eng = get_spatial_dataset_engine(name='default')
        # Data rods parameters
        latlonbox = [post_params['lonW'], post_params['latS'], post_params['lonE'], post_params['latN']]
        time_st = plot_time + ':00:00Z/' + plot_time + ':00:30Z'
        zip_file, store_name, store_id = get_raster_zip(latlonbox, time_st, model, variable)
        # Create raster in geoserver
        flag_add_layer = False
        response = geo_eng.create_coverage_resource(store_id=store_id,
                                                    coverage_file=zip_file,
                                                    coverage_type='worldimage',
                                                    overwrite=True,
                                                    )
        if not response['success']:
            result = geo_eng.create_workspace(workspace_id=get_workspace(),
                                              uri='tethys_app-%s' % get_workspace())
            if result['success']:
                response = geo_eng.create_coverage_resource(store_id=store_id,
                                                            coverage_file=zip_file,
                                                            coverage_type='worldimage',
                                                            overwrite=True,
                                                            )
                if response['success']:
                    flag_add_layer = True
        else:
            flag_add_layer = True

        if flag_add_layer:
            # Add raster to map
            title = '{0} {1}'.format(variable, plot_time)
            geoserver_layer = MVLayer(source='ImageWMS',
                                      options={'url': get_geoserver_url(),
                                               'params': {'LAYERS': store_id},
                                               'serverType': 'geoserver'},
                                      legend_title=title,
                                      )
            map_layers = [geoserver_layer]

    return map_layers