Using external file with Embrio
From PyWPS wiki
Contents |
[edit] Task Information
Summary: We wanna allow user to run process on custom files uploaded via browser
[edit] Vector
[edit] How upload file to the server
Need to define wich tasks should be done by web script and wich one from pywps process. I have splitted in differents phasis to do a better build. Still need to count the input parameter needed by the process
| Element | Kind of data | Description |
|---|---|---|
| Shape path | shp | The shape file we are going to inport in GRASS |
| File name | TXT | The name of the file used inside GRASS |
- Phase 1: done on Web script :
- Browse for the zip on user machine and copy in the REPOSITORY folder (the one that stores zip)
- Extract the file to a "random name folder"
- Ask user for the name of the file he wanna use
- Make a JS check using the XML file to see if the name is already used
- Ask user for a litle description (100 char for example) about the files he is going to upload
- Ask user for feature he wanna inport (line,point,area)
- Update the XML history file, with info on step 3 and step 4
- Running the pywps process
- Phase 2: done on Pywps Process :
- Menaging the file reprojection using ogr2ogr. We can also use directly GRASS, but I haven't found a good way for doing this.
- Check if file come with a prj file and do the reprojection
- Check the user input in web form and use the relative command line settings
- Load file in GRASS
- Copy to the data location
- Clean all *.shp files in tmp folder
[edit] Check to do on the file uploaed
- File is too big (size should be decided)
- All the shp,shx and dbf file are there
- All the files have got the same names
[edit] How menage uploaded files
Once the files are inside GRASS, the menage of them won't be so easy. The better idea I have found, is to use an XML (but should also be a database like sqlite for example) as the history for file upload.
Using DOM, we can edit this file for:
- Found the list of all uploaded files
- Adding a new entry (after the inport file process)
- Removing an entry (after the remove file process)
The list should also be used fot dinamically add a layer to the Ka-map canvas.
This can be a sample of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<vector>
<vector name="Name1" description="abc"/>
<vector name="Name2" description="abc"/>
</vector>
This can be a nice code for adding elements each time i upload a file:
<?php
$import = simplexml_load_file('history.xml');
$vector = $import->addChild('vector');
$vector->addAttribute('name', 'Name3');
$vector->addAttribute('description','acder');
?>
[edit] Running a process using a file uploaded as input
Once the file are inside GRASS data, we should allow user to run process using those files are input. We were thinking for example to the buffer function:
User prompt a shp describing an area, and wanna see the buffer aound that.
In the process form, we can use a droop down menu to read the XML file and show the file user as uploaded. After this, the request is made as usual.
I am planning to use simpleXML to populate the Select Box
[edit] Log of development
Doktoreas 09:59, 5 December 2006 (CET): The webform is ready and the list where select files is ok.
[edit] Code
[edit] Process (import)
#!/usr/bin/python
# Author: Luca Casagrande ( luca.casagrande@gmail.com )
import os,time,string,sys
class Process:
def __init__(self):
self.Identifier = "import"
self.processVersion = "0.1"
self.storeSupport = "true"
self.Title="Import a vector layer "
self.Abstract="Import a vector layer inside the grassdb"
self.grassLocation="/home/geko/progetti/grass/dati/spearfish61/"
self.Inputs = [
# 0
{
'Identifier': 'shp',
'Title': 'Shp',
'ComplexValueReference': {
'Formats':["application/octet-stream"],
},
},
#1
{
'Identifier': 'shx',
'Title': 'Shx',
'ComplexValueReference': {
'Formats':["application/octet-stream"],
},
},
#2
{
'Identifier': 'dbf',
'Title': 'Dbf',
'ComplexValueReference': {
'Formats':["application/octet-stream"],
},
},
#3
{
'Identifier': 'name',
'Title': 'Input name',
'LiteralValue': {
'Formats':["text/xml"],
},
}
]
self.Outputs = [
#0
{
'Identifier': 'output',
'Title': 'Resulting output map',
'ComplexValueReference': {
'Formats':["text/xml"]
}
},
]
def execute(self):
os.rename(self.DataInputs['shp'],"input.shp")
os.rename(self.DataInputs['dbf'],"input.dbf")
os.rename(self.DataInputs['shx'],"input.shx")
os.system("g.mapset mapset=user1")
os.system("v.in.ogr -o dsn=input.shp output=%s min_area=0.0001 snap=-1 --overwrite 1>&2" %\
(self.DataInputs['name']))
os.system("v.out.ogr type=line format=GML input=%s dsn=out.xml olayer=sample.xml 1>&2" %\
(self.DataInputs['name']))
if "out.xml" in os.listdir(os.curdir):
self.Outputs[0]['value'] = "out.xml"
return
else:
return "Output file not created"
[edit] Process (buffer)
#!/usr/bin/python
# Author: Luca Casagrande ( luca.casagrande@gmail.com )
import os,time,string,sys
class Process:
def __init__(self):
self.Identifier = "buffer_vect"
self.processVersion = "0.1"
self.statusSupport = "true"
self.storeSupport = "true"
self.Title="Create a buffer around an assigned vector "
self.Abstract="Create a buffer around an assigned vector"
self.grassLocation="/home/geko/progetti/grass/dati/spearfish61/"
self.Inputs = [
# 0
{
'Identifier': 'vector',
'Title': 'Input vector',
'LiteralValue': {
'Formats':["text/xml"],
},
},
# 1
{
'Identifier': 'radius',
'Title': 'Value of the buffer',
'LiteralValue': {'values':["*"]},
'dataType' : type(0.0),
'value':None
},
]
self.Outputs = [
#0
{
'Identifier': 'output',
'Title': 'Resulting output map',
'ComplexValueReference': {
'Formats':["text/xml"],
},
},
]
def execute(self):
os.system("g.mapset user1 >&2")
os.system("g.region -p")
os.system("v.buffer input=%s output=buff type=point,line,area layer=1 buffer=%s scale=1.0 tolerance=0.01 1>&2" %\
(self.Inputs[0]['value'],self.Inputs[1]['value']))
os.system("v.out.ogr type=area format=GML input=buff dsn=out.xml olayer=path.xml 1>&2")
if "out.xml" in os.listdir(os.curdir):
self.Outputs[0]['value'] = "out.xml"
return
else:
return "Output file not created"

