[Gvsig_english] gvSIG gdbms shape driver

erwan bocher erwan.bocher at gmail.com
Tue Oct 10 17:10:04 CEST 2006


Hi Fran,

Muchas gracias amigo.

Kenavo

On 10/10/06, Francisco José <fpenarru at iver.es> wrote:
>
>  Hi, Erwan.
>
> If you only want to deal with DBF, then you may want to use DBF writer.
> Here you are an example:
>
> package org.gvsig.example.shp;
>
> import java.io.File;
> import java.io.IOException;
> import java.sql.Types;
> import java.util.Calendar;
>
> import com.hardcode.gdbms.engine.data.driver.DriverException;
> import com.hardcode.gdbms.engine.values.Value;
> import com.hardcode.gdbms.engine.values.ValueFactory;
> import com.iver.cit.gvsig.fmap.core.DefaultRow;
> import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
> import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
> import com.iver.cit.gvsig.fmap.drivers.dbf.DBFDriver;
> import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
> import com.iver.cit.gvsig.fmap.edition.EditionException;
>
> /**
>  * @author fjp
>  *
>  * Abrimos un dbf y le añadimos un campo. Lo rellenamos con la fecha
> actual con formato cadena.
>  *
>  */
> public class AddFieldToDBF  {
>
>     public static void main(String[] args) {
>         AddFieldToDBF prueba = new AddFieldToDBF();
>         prueba.execute("");
>     }
>
>     public void execute(String actionCommand) {
>         DBFDriver driver = new DBFDriver();
>         File myFile = new File("c:/0libro.dbf");
>         try {
>             // Abrimos el fichero
>             driver.open(myFile);
>
>             // Comprobamos que tenemos acceso de escritura.
>             if (driver.canSaveEdits())
>             {
>
>                 // Creamos el nuevo campo
>
>                 FieldDescription dateField = new FieldDescription();
>                 dateField.setFieldName("Date");
>                 dateField.setFieldType(Types.VARCHAR);
>                 dateField.setFieldLength(20);
>
>                 // Creamos un array con el nuevo campo
>                 ITableDefinition tableDef = driver.getTableDefinition();
>                 FieldDescription[] oldFields = tableDef.getFieldsDesc();
>                 int numOldFields = oldFields.length;
>                 FieldDescription[] newFields = new
> FieldDescription[numOldFields + 1];
>                 System.arraycopy(oldFields, 0, newFields, 0,
> numOldFields);
>                 newFields[numOldFields] = dateField;
>
>                 tableDef.setFieldsDesc(newFields);
>
>                 driver.initialize(tableDef);
>                 driver.preProcess();
>                 Value[] att = new Value[newFields.length];
>                 Calendar today = Calendar.getInstance();
>                 for (int i=0; i < driver.getRowCount(); i++)
>                 {
>                     for (int j=0; j < numOldFields; j++)
>                     {
>                         att[j] = driver.getFieldValue(i, j);
>                     }
>                     att[numOldFields] = ValueFactory.createValue(
> today.getTime().toLocaleString());
>                     DefaultRow row = new DefaultRow(att);
>                     DefaultRowEdited edRow = new DefaultRowEdited(row,
>                             DefaultRowEdited.STATUS_MODIFIED, i);
>
>                     driver.process(edRow);
>                 }
>                 driver.postProcess();
>
>                 System.out.println("Fichero modificado");
>
>             }
>             else
>             {
>                 System.err.println("El fichero no tiene permiso de
> edición");
>             }
>             driver.close();
>         } catch (IOException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         } catch (EditionException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         } catch (DriverException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         }
>
>
>
>     }
>
>
> }
>
> Have fun.
>
>
>
> erwan bocher escribió:
>
> Hi,
>
> Thanks for the answer.I don't understand why I should used layer object.
> I need a simple way to modify attributes values and fields in a shapefile
> without gvSIG. I thought to do it with GDBMS.
>
> Sorry for log file.
>
> R1.
>
>
>
> On 10/10/06, Vicente Caballero Navarro <vicente.caballero at iver.es > wrote:
> >
> >  Hello Erwan,
> >
> > We need to see the ".log" with the trace of the error to be able to help
> > you in the future. Nevertheless, with the code that you have sent a
> > "NullPointerException" exists as a minimum in the object "field", that is
> > null.
> >
> > You should create the "LayerDefinition" and to add him to the the fields
> > that consider, to initialize the "Writer" and finally to stop the edition.
> >
> > I include you a piece of code by if helps you:
> >
> >   ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition
> > (layer);
> >
> >     //Here, modify the LayerDefinition
> >
> >   writer.initialize(lyrDef);
> >   vea.stopEdition(writer, EditionEvent.GRAPHIC);
> >
> > I expect to have clarified something.
> >
> > Regards.
> >
> > Vicente Caballero Navarro
> > Grupo desarrollo gvSIG.
> > www.gvsig.gva.es
> > www.iver.es
> >
> >  ----- Original Message -----
> > *From:* erwan bocher <erwan.bocher at gmail.com>
> > *To:* Users and Developers mailing list<gvsig_internacional at runas.cap.gva.es>
> > *Sent:* Tuesday, October 10, 2006 9:53 AM
> > *Subject:* [Gvsig_english] gvSIG gdbms shape driver
> >
> >  Hello,
> >
> >
> > I try to use GDBMS to read and write values and fields in a shapefile. I
> > used the code below. I have an error about
> > com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver.
> >
> > //Ici on va chercher le ou les drivers necessaires driver
> >         LayerFactory.setDriversPath("C:/Temp/driver");
> >
> >         Driver driver = LayerFactory.getDM().getDriver("gvSIG shp
> > driver");
> >
> >           if (driver!=null){ //Si le driver est trouvé alors on continue
> >
> >
> >               System.out.println(driver.getName()); //On affiche son
> > petit nom.
> >
> >             LayerFactory.getDataSourceFactory().addFileDataSource("gvSIG
> > shp driver", "macouche",
> >             "C:/Temp/countries.shp");
> >
> >             // SelectableDataSource permet de travailler sur la table
> >
> >             SelectableDataSource sds = new SelectableDataSource(
> > LayerFactory.getDataSourceFactory().createRandomDataSource("macouche",
> > DataSourceFactory.MANUAL_OPENING));
> >             EditableAdapter ea = new EditableAdapter();
> >             ea.setOriginalDataSource(sds);
> >
> >             ea.startEdition(0);
> >             IWriter writer = ea.getWriter();
> >
> >             FieldDescription field = null;
> >
> >             field.setFieldName("myfield");
> >
> >             ea.addField(field);
> >
> >             ea.stopEdition(writer, 0);
> >
> > Thanks
> >
> > R1.
> >
> >
> >
> >   ------------------------------
> > _______________________________________________
> > Gvsig_internacional mailing list
> > Gvsig_internacional at runas.cap.gva.es
> > http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
> >
> >
> > _______________________________________________
> > Gvsig_internacional mailing list
> > Gvsig_internacional at runas.cap.gva.es
> > http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
> >
> >
> >
> ------------------------------
>
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at runas.cap.gva.es
> http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
>
>
>
> --
> Francisco José Peñarrubia
> Equipo gvSIG
>
> IVER T.I. S.A.
> c/Salamanca 50
> 46005 Valencia
> Spain
>
>
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at runas.cap.gva.es
> http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://runas.cap.gva.es/pipermail/gvsig_internacional/attachments/20061010/28222a12/attachment.htm


More information about the Gvsig_internacional mailing list