DAL desde la perspectiva del cliente de la librería
En esta presentación daremos una introducción a la librería y su arquitectura, describiendo el manejo de acceso a datos tabulares. No se describirá el acceso a datos raster.
Desde aquí puede acceder a las diapositivas del seminario relacionada con el API de DAL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DataManager manager;
StoreParameters params;
FeatureStore store;
FeatureSet features;
Feature feature;
manager = DALLocator.getDataManager();
params = manager.createStoreParameters("Shape")
params.setDynValue("shpfilename","data/prueba.shp");
store = (FeatureStore)manager.createStore(params);
features = store.getFeatureSet();
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(feature.getString("NOMBRE"));
}
it.dispose();
features.dispose();
store.dispose();
|
CódigoFuente 1: Acceso a un fichero Shape
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | DataManager manager;
StoreParameters params;
FeatureStore store;
FeatureSet features;
Feature feature;
manager = DALLocator.getDataManager();
params = manager.createStoreParameters("PostgreSQL");
params.setDynValue("host", SERVER_IP);
params.setDynValue("dbuser",SERVER_USER);
params.setDynValue("password",SERVER_PASWD);
params.setDynValue("dbname",SERVER_DBNAME);
params.setDynValue("table","prueba");
store = (FeatureStore)manager.createStore(params);
features = store.getFeatureSet();
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(feature.getString("nombre"));
}
it.dispose();
features.dispose();
store.dispose();
|
CódigoFuente 2: Acceso a una tabla PostgreSQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Iterator typeIt = store
.getFeatureTypes().iterator();
while( typeIt.hasNext() ) {
FeatureQuery query = store
.createFeatureQuery();
FeatureType type = (FeatureType)typeIt.next();
query.setFeatureType(type);
features = store.getFeatureSet(query);
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(
feature.getString("NOMBRE"));
}
it.dispose();
features.dispose();
}
|
CódigoFuente 3: Filtrado por “Tipo” de fenómeno
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | FeatureQuery query = store
.createFeatureQuery();
Evaluator filter = manager
.createExpresion("NOMBRE like 'a%'");
query.setFilter(filter);
features = store.getFeatureSet(query);
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(
feature.getString("NOMBRE"));
}
it.dispose();
features.dispose();
|
CódigoFuente 4: Filtrado por expresión
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | FeatureQuery query = store
.createFeatureQuery();
query.getOrder().add("CODIGO",true);
query.getOrder().add("NOMBRE",false);
features = store.getFeatureSet(query);
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(
feature.getString("NOMBRE"));
}
it.dispose();
features.dispose();
|
CódigoFuente 5: Ordenación por nombre
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | FeatureQuery query = store
.createFeatureQuery();
Evaluator nombreToLower = manager
.createExpresion("toLower(NOMBRE)");
query.getOrder().add("CODIGO",true);
query.getOrder().add(nombreToLower,false);
features = store.getFeatureSet(query);
DisposableIterator it = features.iterator();
while( it.hasNext() ) {
feature = (Feature)it.next();
System.out.println(
feature.getString("NOMBRE"));
}
it.dispose();
features.dispose();
|
CódigoFuente 6: Ordenación por expresión
1 2 3 4 5 6 7 8 | store.edit();
EditableFeature feature = store.createNewFeature();
feature.set("NOMBRE", "Burjasot");
feature.set("TIPO", "MUNICIPIO");
store.insert(feature);
store.finishEditing();
|
CódigoFuente 7: Inserción de un fenómeno
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | store.edit();
EditableFeature feature;
FeatureQuery query = store.createFeatureQuery();
query.setFilter( manager.createExpresion("NOMBRE = 'Burjasot'") );
features = store.getFeatureSet(query);
Iterator it = features.iterator();
while( it.hasNext() ) {
feature = ((Feature)it.next()).getEditable();
feature.set("TIPO", "Municipio");
features.update(feature);
}
features.dispose();
store.finishEditing();
|
CódigoFuente 8: Modificación de fenómenos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | store.edit();
FeatureQuery query = store.createFeatureQuery();
query.setFilter( manager.createExpresion("NOMBRE = 'Burjasot'") );
features = store.getFeatureSet(query);
Iterator it = features.iterator();
while( it.hasNext() ) {
it.next();
it.remove();
}
features.dispose();
store.finishEditing();
|
CódigoFuente 9: Eliminación de fenómenos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | FeatureType featureType = store.getDefaultFeatureType();
Iterator it = featureType.iterator();
while( it.hasNext() ) {
attribute = (FeatureAttributeDescriptor)it.next();
System.out.print(attribute.getDataTypeName();
if( attribute.getSize() > 1 ) {
System.out.print("["+attribute.getSize()+"]");
}
System.out.print(" " + attribute.getName() );
Evaluator eval = attribute.getEvaluator();
if( eval != null ) {
System.out.print(attribute.getName()+", "+attribute.getDataTypeName()+" -- Calculado "+ eval.getCQL());
}
System.out.println();
}
|
CódigoFuente 10: Consultar la estructura de un almacén
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | DataExplorerParameters eparams = manager
.createServerExplorerParameters("FilesystemExplorer");
eparams.setDynValue("initialpath","/data");
DataServerExplorer serverExplorer = manager.createServerExplorer(eparams);
NewFeatureStoreParameters sparams = (NewFeatureStoreParameters)
serverExplorer.getAddParameters("DBF");
sparams.setDynValue("dbffilename","prueba.dbf");
EditableFeatureType featureType = (EditableFeatureType)
sparams.getDefaultFeatureType();
featureType.add("NOMBRE", DataTypes.STRING,100);
featureType.add("MUNICIPIO", DataTypes.STRING,100);
featureType.add("POBLACION", DataTypes.LONG);
featureType.add("AREA", DataTypes.DOUBLE);
serverExplorer.add(sparams);
|
CódigoFuente 11: Creación de un nuevo almacén
1 2 3 4 5 6 7 | store.edit();
FeatureType featureType = store.getDefaultFeatureType();
EditableFeatureType editableFeatureType = featureType.getEditable();
editableFeatureType.getAttributeDescriptor("MUNICIPIO").setSize(50);
editableFeatureType.getAttributeDescriptor("NOMBRE").setSize(50);
store.update(editableFeatureType);
store.finishEdition()
|
CódigoFuente 12: Modificación de la estructura de un almacén
1 2 3 4 5 6 7 8 9 | store.edit();
FeatureType featureType = store.getDefaultFeatureType();
EditableFeatureType editableFeatureType = featureType.getEditable();
editableFeatureType.add("DENSIDAD_POBLACION",
DataTypes.DOUBLE,
manager.createExpresion("POBLACION / AREA")
);
store.update(editableFeatureType);
store.finishEditing();
|
CódigoFuente 13: Añadir atributo calculado a un almacén
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class XY2Geometry extends AbstractEvaluator {
private String xname;
private String yname;
private GeometryManager geomManager;
public XY2Geometry initialize(String xname, String yname) {
this.xname = xname;
this.yname = yname;
geomManager = GeometryLocator.getGeometryManager();
return this;
}
public Object evaluate(EvaluatorData data) throws EvaluatorException {
Double x = (Double) data.getDataValue(this.xname);
Double y = (Double) data.getDataValue(this.yname);
Geometry geom;
try {
geom = geomManager.createPoint(x.doubleValue(), y
.doubleValue(), Geometry.SUBTYPES.GEOM2D);
} catch (CreateGeometryException e) {
throw new EvaluatorException(e);
}
return geom;
}
public String getName() {
return "XY2Geometry";
}
}
|
CódigoFuente 14: Evaluado para atributo calculado personalizado
1 2 3 4 5 6 7 8 9 | store.edit();
FeatureType featureType = store.getDefaultFeatureType();
EditableFeatureType editableFeatureType = featureType.getEditable();
editableFeatureType.add("GEOM",
DataTypes.GEOMETRY,
new XY2Geometry().initialize("X", "Y")
);
store.update(editableFeatureType);
store.finishEditing();
|
CódigoFuente 15: Añadir atributo calculado personalizado (CódigoFuente 14) a un almacén
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | public class MyTransform extends AbstractFeatureStoreTransform {
private String geomName;
private String xname;
private String yname;
private GeometryManager geomManager;
public MyTransform() {
this.geomManager = GeometryLocator.getGeometryManager();
}
public MyTransform initialize(FeatureStore store, String geomName,
String xname, String yname) throws DataException {
setFeatureStore(store);
this.geomName = geomName;
this.xname = xname;
this.yname = yname;
EditableFeatureType type = getFeatureStore().getDefaultFeatureType()
.getEditable();
type.add(geomName, DataTypes.GEOMETRY);
List list = new ArrayList(1);
list.add(type.getNotEditableCopy());
setFeatureTypes(list, (FeatureType) list.get(0));
return this;
}
public void applyTransform(Feature source, EditableFeature target)
throws DataException {
target.copyFrom(source);
Geometry geom;
try {
geom = geomManager.createPoint(source.getDouble(xname), source
.getDouble(yname), Geometry.SUBTYPES.GEOM2D);
} catch (CreateGeometryException e) {
throw new ReadException("XYTranform", e);
}
target.setGeometry(this.geomName, geom);
}
public void saveToState(PersistentState state) throws
PersistenceException {
state.set("xname", xname);
state.set("yname", yname);
state.set("geomName", geomName);
}
public void loadFromState(PersistentState state)
throws PersistenceException {
this.xname = state.getString("xname");
this.yname = state.getString("yname");
this.geomName = state.getString("geomName");
}
public FeatureType getSourceFeatureTypeFrom(
FeatureType targetFeatureType) {
EditableFeatureType ed = targetFeatureType.getEditable();
ed.remove(this.geomName);
return ed.getNotEditableCopy();
}
public boolean isTransformsOriginalValues() {
return false;
}
}
|
CódigoFuente 16: Implementación de una transformación de almacén
1 2 3 | FeatureStoreTransform transform = new MyTransform()
.initialize(store,"geom","x","y");
store.getTransforms().add(transform)
|
CódigoFuente 17: Aplicar una transformación (CódigoFuente 16) a un almacén