for the bush program

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

for the bush program

JINHUI
Dears:

I want to creat a bush program
how can I import a .csv and a .shp file and then to creat it in the the 'Beanshell'
I try like this:


import org.csv.Name_csv;
import org.shp.core.Name_shp.MapContext;
MapContext mc = mc;

SQL("create table FP_ART_elps as select id,Time,Wdir,z0,KM_p01a,KM_p01b,KM_p01c, ST_makeellipse((XC:: double), (YC:: double), (longueur:: double), (largeur:: double), (rotation:: double)+pi()/2)as footprint from ART_g26;");
SQL("create table veg_in_elps as select b.Time, ST_area(ST_Intersection(a.the_geom,b.footprint)) as the_geom from super_9 a,FP_ART_elps b where ST_Intersects(a.the_geom,b.footprint);");
SQL("create table SURF_elps as select Time, st_area(footprint) as surf_elps from FP_ART_elps;");
SQL("create table Surf_veg_inE as select Time,Sum(the_geom) as surf_veg from veg_in_elps group by Time;");
SQL("create table elps_veg as select a.Time,a.surf_elps,b.surf_veg from SURF_elps as a,Surf_veg_inE as b where a.Time=b.Time;");

but it not works,and Iwant to learn it more and I find

import org.gdms.data.DataSource;
import org.orbisgis.core.layerModel.MapContext;
 
/** Spécifiez ici le nom de la couche qui va servir de pochoir. Dans cet exemple, la couche pochoir s'appelle agglo_nantes */
zone = "agglo_nantes";
 
/** Spécifie ici le suffixe qui sera généré à la fin de chacunes des couches découpées */
suffixe = "_agglo";
 
MapContext mc = mc;
print(mc.getLayerModel().getLayerCount());
 
for (int i = 0; i< mc.getLayerModel().getLayerCount(); i++)
{
print(mc.getLayerModel().getLayer(i).getName());
layerName = mc.getLayerModel().getLayer(i).getName();
layerNameOut = layerName + suffixe;
 
/** Retrouvez ici la requete SQL qui découpe les couches sur la base du pochoir */
SQL("CREATE TABLE " + layerNameOut + " as select st_intersection(a.the_geom,b.the_geom) as the_geom, a.*{except the_geom}
from " + layerName + " a, " + zone + " b where st_intersects(a.the_geom, b.the_geom);");
 
/** A la place de /tmp/, mettez l'adresse du dossier ou vos couches générées doivent atterir (ici elles seront en .shp) */
SQL("SELECT EXPORT('"+ layerNameOut+"', '/tmp/"+ layerNameOut +".shp');");

but I  can not understand ,Can you show my example for me?

thanks!!!


JINHUI
Reply | Threaded
Open this post in threaded view
|

Re: for the bush program

Gwendall Petit
Administrator
Hi,

If you want to open a .csv file (or a .shp), there is a specific SQL function for that
EXECUTE register ('path_to_file');
or
EXECUTE register ('path_to_file', 'name');"
The specified file will be declared in the Geocatalog, and you will be able to process your SQL script.

So if it fits to your need, you don't need beanshell.

Regards,

Gwendall
Reply | Threaded
Open this post in threaded view
|

Re: for the bush program

JINHUI
Hello,

I would like to ask a problem about the bush program :
How can I import the .csv and the shapefile ,

and I want to write a loop that can Run automatically:


one file is ART_260.csv
another is super_9.shp


import org.gdms.data.DataSource;
import org.orbisgis.core.layerModel.MapContext;
zone = "super_9";
suffixe = "_csv";
MapContext mc = mc;
print(mc.getLayerModel().getLayerCount());
 
for (int i = 0; i< mc.getLayerModel().getLayerCount(); i++)
{
print(mc.getLayerModel().getLayer(i).getName());
layerName = mc.getLayerModel().getLayer(i).getName();
layerNameOut = layerName + suffixe;
 
SQL("create table FP_ART_elps as select id,Time,Wdir,z0,KM_p01a,KM_p01b,KM_p01c, ST_makeellipse((XC:: double), (YC:: double), (longueur:: double), (largeur:: double), (rotation:: double)+pi()/2)as footprint from ART_g26;
SQL(create table veg_in_elps as select b.Time, ST_area(ST_Intersection(a.the_geom,b.footprint)) as the_geom from super_9 a,FP_ART_elps b where ST_Intersects(a.the_geom,b.footprint);
 
SQL(create table SURF_elps as select Time, st_area(footprint) as surf_elps from FP_ART_elps;
SQL(create table Surf_veg_inE as select Time,Sum(the_geom) as surf_veg from veg_in_elps group by Time;
SQL(create table elps_veg as select a.Time,a.surf_elps,b.surf_veg from SURF_elps as a,Surf_veg_inE as b where a.Time=b.Time;
SQL(alter table elps_veg add column veg_Ftion double;
SQL(update elps_veg set veg_Ftion=(surf_veg/surf_elps);
SQL("SELECT EXPORT('"+ layerNameOut+"', '/shp/"+ layerNameOut +".csv');");
}

Accoding the example in http://doc.orbisgis.org/3.0:beanshell_crop_vector

I use Orbisgis4.0, I am not  understand it,can you explan to me !!!


Best regards
jinhui chen
Reply | Threaded
Open this post in threaded view
|

Re: for the bush program

Gwendall Petit
Administrator
I'm not expert in Beanshell but below is what I'm able to explain :

In the exemple shown here,
- zone is a vector layer present in the Geocatalog
- suffixe is a text parameter that you can adapt to your needs. This text will be added to all layers created in the next steps
- layername is a variable that stores the name of all layers listed in the TOC (the zone layer should not be present here)
- layerNameOut is a concatenation of layername and zone parameters

Then you have your SQL script. Note that the correct syntax to use SQL in beanshell is
SQL(" your instruction");
if you want to insert some parameters in your instruction, you should use this
SQL(" my instruction" + my_paramater + "end_of_my_instruction");
--> Be careful, in your example, you made some errors ... you forgot a lot of " at the begin of your SQL instruction

Hope it helps

Regards

Gwendall
Reply | Threaded
Open this post in threaded view
|

Re: for the bush program

JINHUI
Dear professor Gwendall,

As what your said:

I correct my SQL like those:

import org.gdms.data.DataSource;
import org.orbisgis.core.layerModel.MapContext;
 
zone = "super_9";
 
suffixe = "_csv";
 
MapContext mc = mc;
print(mc.getLayerModel().getLayerCount());
 
for (int i = 0; i< mc.getLayerModel().getLayerCount(); i++)
{ print(mc.getLayerModel().getLayer(i).getName()); layerName = mc.getLayerModel().getLayer(i).getName(); layerNameOut = layerName + suffixe;
SQL("create table " + FP_ART_elps + " as select id,Time,Wdir,z0,KM_p01a,KM_p01b,KM_p01c, ST_makeellipse((XC:: double), (YC:: double), (longueur:: double), (largeur:: double), (rotation:: double)+pi()/2)as footprint from " + ART_g26 + ";");
SQL("create table " + veg_in_elps + " as select b.Time, ST_area(ST_Intersection(a.the_geom,b.footprint)) as the_geom from " + super_9 + " a, " + FP_ART_elps + " b where ST_Intersects(a.the_geom,b.footprint);");
SQL("create table " + SURF_elps + " as select Time, st_area(footprint) as surf_elps from " + FP_ART_elps + ";");
SQL("create table " + Surf_veg_inE + " as select Time,Sum(the_geom) as surf_veg from " + veg_in_elps + " group by Time;");
SQL("create table " + elps_veg + " as select a.Time,a.surf_elps,b.surf_veg from " + SURF_elps + " as a,Surf_veg_inE as b where a.Time=b.Time;");
SQL("alter table " + elps_veg + " add column veg_Ftion double;");
SQL("update " + elps_veg + " set veg_Ftion=(surf_veg/surf_elps);");

SQL("SELECT EXPORT('"+ elps_veg +"', '/tmp/"+ elps_veg +".csv');");
}


and some erros like those:

ERROR [AWT-EventQueue-0] (BshConsolePanel.java:428) - The script is not valid
bsh.EvalError: Sourced file: inline evaluation of: ``import org.gdms.data.DataSource; import org.orbisgis.core.layerModel.MapContext; . . . '' : Command not found: SQL( java.lang.String ) : at Line: 15 : in file: inline evaluation of: ``import org.gdms.data.DataSource; import org.orbisgis.core.layerModel.MapContext; . . . '' : SQL ( "create table " + FP_ART_elps + " as select id,Time,Wdir,z0,KM_p01a,KM_p01b,KM_p01c, ST_makeellipse((XC:: double), (YC:: double), (longueur:: double), (largeur:: double), (rotation:: double)+pi()/2)as footprint from " + ART_g26 + ";" )

        at bsh.Name.invokeLocalMethod(Name.java:952)
        at bsh.Name.invokeMethod(Name.java:804)
        at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
        at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
        at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
        at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
        at bsh.BSHBlock.eval(BSHBlock.java:80)
        at bsh.BSHBlock.eval(BSHBlock.java:46)
        at bsh.BSHForStatement.eval(BSHForStatement.java:111)
        at bsh.Interpreter.eval(Interpreter.java:664)
        at bsh.Interpreter.eval(Interpreter.java:758)
        at bsh.Interpreter.eval(Interpreter.java:747)
        at org.orbisgis.view.beanshell.BshConsolePanel.onExecute(BshConsolePanel.java:412)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor17.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        at java.beans.EventHandler.invokeInternal(Unknown Source)
        at java.beans.EventHandler.access$000(Unknown Source)
        at java.beans.EventHandler$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.beans.EventHandler.invoke(Unknown Source)
        at $Proxy41.actionPerformed(Unknown Source)
        at org.orbisgis.view.components.actions.DefaultAction.actionPerformed(DefaultAction.java:133)
        at org.orbisgis.view.docking.internals.actions.CButtonExt.action(CButtonExt.java:29)
        at bibliothek.gui.dock.common.action.CButton$1.actionPerformed(CButton.java:82)
        at bibliothek.gui.dock.action.actions.SimpleButtonAction.action(SimpleButtonAction.java:113)
        at bibliothek.gui.dock.themes.basic.action.BasicButtonHandler.triggered(BasicButtonHandler.java:48)
        at bibliothek.gui.dock.themes.basic.action.BasicButtonModel.trigger(BasicButtonModel.java:704)
        at bibliothek.gui.dock.themes.basic.action.BasicButtonModel$Listener.mouseReleased(BasicButtonModel.java:742)
        at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)




Q1:insert parameters=>update " + elps_veg + " set "+ veg_Ftion + "=(surf_veg/surf_elps) add" + + " on the veg_Ftion?
Q2:how can I coorect it?it's the problem of the import(import org.gdms.data.DataSource;
import org.orbisgis.core.layerModel.MapContext;)?
Q3:How to reprensent the 0.3666 to37% in SQL;(update elps_veg set veg_Ftion=(surf_veg/surf_elps)*100*'%')?

kind regards

Jinhui