Skip to content

Commit

Permalink
read CRS from ili (claeis#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
claeis committed Nov 23, 2017
1 parent 5bbe688 commit 434307b
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 62 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ ideas/open issues/questions
LATEST
---------------------------

ili2pg 3.11.0 (2017-11-xx)
-----------------------------
- added MultiPoint smart mapping
- read CRS from ili
- ili2c-4.7.5
- iox-ili-1.20.0

ili2pg 3.10.10 (2017-11-03)
-----------------------------
- added mssql support
Expand Down
144 changes: 144 additions & 0 deletions ili2pg/test/java/ch/ehi/ili2pg/CrsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ch.ehi.ili2pg;

import static org.junit.Assert.*;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ch.ehi.basics.logging.EhiLogger;
import ch.ehi.ili2db.base.DbUrlConverter;
import ch.ehi.ili2db.base.Ili2db;
import ch.ehi.ili2db.base.Ili2dbException;
import ch.ehi.ili2db.gui.Config;
import ch.ehi.ili2db.mapping.NameMapping;
import ch.interlis.iox.IoxException;

//-Ddburl=jdbc:postgresql:dbname -Ddbusr=usrname -Ddbpwd=1234
public class CrsTest {
private static final String DBSCHEMA = "Crs";
String dburl=System.getProperty("dburl");
String dbuser=System.getProperty("dbusr");
String dbpwd=System.getProperty("dbpwd");
Connection jdbcConnection=null;
Statement stmt=null;
@After
public void endDb() throws Exception
{
if(jdbcConnection!=null){
jdbcConnection.close();
}
}
public Config initConfig(String xtfFilename,String dbschema,String logfile) {
Config config=new Config();
new ch.ehi.ili2pg.PgMain().initConfig(config);
config.setDburl(dburl);
config.setDbusr(dbuser);
config.setDbpwd(dbpwd);
if(dbschema!=null){
config.setDbschema(dbschema);
}
if(logfile!=null){
config.setLogfile(logfile);
}


config.setXtffile(xtfFilename);
if(xtfFilename!=null && Ili2db.isItfFilename(xtfFilename)){
config.setItfTransferfile(true);
}
return config;

}
private int getColumnCrsId(String schemaName, String tableName, String attrName, Connection db) throws SQLException {
String queryBuild="SELECT srid FROM geometry_columns WHERE "
+(schemaName!=null?"f_table_schema='"+schemaName.toLowerCase()+"' AND ":"")
+"f_table_name='"+tableName.toLowerCase()+"' "
+"AND f_geometry_column='"+attrName.toLowerCase()+"'";
Statement stmt=null;
ResultSet rs=null;
try {
stmt = db.createStatement();
rs=stmt.executeQuery(queryBuild);
if(!rs.next()) {
throw new SQLException("no column in given table");
}
int crs=rs.getInt(1);
return crs;
}finally {
if(rs!=null) {
rs.close();
rs=null;
}
if(stmt!=null) {
stmt.close();
stmt=null;
}
}
}

@Test
public void importIliCoord() throws Exception
{
Class driverClass = Class.forName("org.postgresql.Driver");
jdbcConnection = DriverManager.getConnection(dburl, dbuser, dbpwd);
stmt=jdbcConnection.createStatement();
stmt.execute("DROP SCHEMA IF EXISTS "+DBSCHEMA+" CASCADE");
File data=new File("test/data/Crs/CrsCoord23.ili");
Config config=initConfig(data.getPath(),DBSCHEMA,data.getPath()+".log");
config.setFunction(Config.FC_SCHEMAIMPORT);
config.setCreateFk(config.CREATE_FK_YES);
config.setCreateNumChecks(true);
config.setTidHandling(Config.TID_HANDLING_PROPERTY);
config.setBasketHandling(config.BASKET_HANDLING_READWRITE);
config.setCatalogueRefTrafo(null);
config.setMultiSurfaceTrafo(null);
config.setMultilingualTrafo(null);
config.setInheritanceTrafo(null);
config.setDefaultSrsCode("4326");
//Ili2db.readSettingsFromDb(config);
Ili2db.run(config,null);
assertEquals(21781,getColumnCrsId(DBSCHEMA, "classa1", "attrLV03", jdbcConnection));
assertEquals(2056,getColumnCrsId(DBSCHEMA, "classa1", "attrLV95", jdbcConnection));
}
@Test
public void importXtfCoord() throws Exception
{
Class driverClass = Class.forName("org.postgresql.Driver");
jdbcConnection = DriverManager.getConnection(dburl, dbuser, dbpwd);
stmt=jdbcConnection.createStatement();
stmt.execute("DROP SCHEMA IF EXISTS "+DBSCHEMA+" CASCADE");

EhiLogger.getInstance().setTraceFilter(false);
File data=new File("test/data/Crs/CrsCoord23a.xtf");
Config config=initConfig(data.getPath(),DBSCHEMA,data.getPath()+".log");
config.setFunction(Config.FC_IMPORT);
config.setCreateFk(config.CREATE_FK_YES);
config.setCreateNumChecks(true);
config.setTidHandling(Config.TID_HANDLING_PROPERTY);
config.setBasketHandling(config.BASKET_HANDLING_READWRITE);
config.setCatalogueRefTrafo(null);
config.setMultiSurfaceTrafo(null);
config.setMultilingualTrafo(null);
config.setInheritanceTrafo(null);
config.setDefaultSrsCode("4326");
//Ili2db.readSettingsFromDb(config);
try{
Ili2db.run(config,null);
assertEquals(21781,getColumnCrsId(DBSCHEMA, "classa1", "attrLV03", jdbcConnection));
assertEquals(2056,getColumnCrsId(DBSCHEMA, "classa1", "attrLV95", jdbcConnection));
}catch(Exception ex){
EhiLogger.logError(ex);
Assert.fail();
}

}
}
33 changes: 30 additions & 3 deletions src/ch/ehi/ili2db/converter/AbstractRecordConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ch.interlis.ili2c.metamodel.LineType;
import ch.interlis.ili2c.metamodel.NumericType;
import ch.interlis.ili2c.metamodel.NumericalType;
import ch.interlis.ili2c.metamodel.SurfaceOrAreaType;
import ch.interlis.ili2c.metamodel.Table;
import ch.interlis.ili2c.metamodel.TransferDescription;
import ch.interlis.ili2c.metamodel.Viewable;
Expand Down Expand Up @@ -121,16 +122,42 @@ public DbColGeometry generatePolylineType(LineType type, String attrName) {
compoundCurve=true;
}
ret.setType(compoundCurve ? DbColGeometry.COMPOUNDCURVE : DbColGeometry.LINESTRING);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
Domain coordDomain=type.getControlPointDomain();
if(coordDomain!=null){
CoordType coord=(CoordType)coordDomain.getType();
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attrName);
}
return ret;
}
public void setCrs(DbColGeometry ret,AttributeDef attr) {
ch.interlis.ili2c.metamodel.Element attrOrDomainDef=attr;
ch.interlis.ili2c.metamodel.Type attrType=attr.getDomain();
if(attrType instanceof ch.interlis.ili2c.metamodel.TypeAlias) {
attrOrDomainDef=((ch.interlis.ili2c.metamodel.TypeAlias)attrType).getAliasing();
attrType=((Domain) attrOrDomainDef).getType();
}
CoordType coord=null;
if(attrType instanceof CoordType) {
coord=(CoordType)attrType;
}else if(attrType instanceof LineType) {
Domain coordDomain=((LineType)attrType).getControlPointDomain();
if(coordDomain!=null){
attrOrDomainDef=coordDomain;
coord=(CoordType)coordDomain.getType();
}
}
if(coord!=null) {
String crs=coord.getCrs(attrOrDomainDef);
if(crs!=null) {
String crsv[]=crs.split(":");
ret.setSrsAuth(crsv[0]);
ret.setSrsId(crsv[1]);
return;
}
}
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
}
public DbColId addKeyCol(DbTable table) {
DbColId dbColId=new DbColId();
Expand Down
42 changes: 20 additions & 22 deletions src/ch/ehi/ili2db/fromili/FromIliRecordConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
}else if (type instanceof PolylineType){
String attrName=attr.getContainer().getScopedName(null)+"."+attr.getName();
DbColGeometry ret = generatePolylineType((PolylineType)type, attrName);
setCrs(ret,attr);
dbCol=ret;
}else if (type instanceof SurfaceOrAreaType){
if(createItfLineTables){
Expand All @@ -476,9 +477,8 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
curvePolygon=true;
}
ret.setType(curvePolygon ? DbColGeometry.CURVEPOLYGON : DbColGeometry.POLYGON);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
// get crs from ili
setCrs(ret,attr);
CoordType coord=(CoordType)((SurfaceOrAreaType)type).getControlPointDomain().getType();
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
Expand All @@ -491,9 +491,8 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
ret.setName(sqlName);
ret.setType(DbColGeometry.POINT);
setNullable(aclass,attr, ret);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
// get crs from ili
setCrs(ret,attr);
ret.setDimension(2); // always 2 (even if defined as 3d in ili)
CoordType coord=(CoordType)((SurfaceOrAreaType)type).getControlPointDomain().getType();
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
Expand All @@ -503,9 +502,8 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
}else if (type instanceof CoordType){
DbColGeometry ret=new DbColGeometry();
ret.setType(DbColGeometry.POINT);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
// get crs from ili
setCrs(ret,attr);
CoordType coord=(CoordType)type;
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
Expand Down Expand Up @@ -537,10 +535,10 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
curvePolygon=true;
}
ret.setType(curvePolygon ? DbColGeometry.MULTISURFACE : DbColGeometry.MULTIPOLYGON);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
SurfaceType surface=((SurfaceType) ((AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfSurfacesAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getSurfaceAttrName())).getDomainResolvingAliases());
// get crs from ili
AttributeDef surfaceAttr = (AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfSurfacesAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getSurfaceAttrName());
setCrs(ret,surfaceAttr);
SurfaceType surface=((SurfaceType) surfaceAttr.getDomainResolvingAliases());
CoordType coord=(CoordType)surface.getControlPointDomain().getType();
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
Expand All @@ -556,11 +554,11 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
curvePolyline=true;
}
ret.setType(curvePolyline ? DbColGeometry.MULTICURVE : DbColGeometry.MULTILINESTRING);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
PolylineType surface=((PolylineType) ((AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfLinesAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getLineAttrName())).getDomainResolvingAliases());
CoordType coord=(CoordType)surface.getControlPointDomain().getType();
// get crs from ili
AttributeDef polylineAttr = (AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfLinesAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getLineAttrName());
setCrs(ret,polylineAttr);
PolylineType polylineType=((PolylineType) polylineAttr.getDomainResolvingAliases());
CoordType coord=(CoordType)polylineType.getControlPointDomain().getType();
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
dbCol=ret;
Expand All @@ -571,10 +569,10 @@ public void generateAttr(DbTable dbTable,Viewable aclass,AttributeDef attr)
MultiPointMapping attrMapping=multiPointAttrs.getMapping(attr);
DbColGeometry ret=new DbColGeometry();
ret.setType(DbColGeometry.MULTIPOINT);
// TODO get crs from ili
ret.setSrsAuth(defaultCrsAuthority);
ret.setSrsId(defaultCrsCode);
CoordType coord=(CoordType) ( ((AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfPointsAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getPointAttrName())).getDomainResolvingAliases());
// get crs from ili
AttributeDef coordAttr = (AttributeDef) ((CompositionType) ((AttributeDef) ((CompositionType) type).getComponentType().getElement(AttributeDef.class, attrMapping.getBagOfPointsAttrName())).getDomain()).getComponentType().getElement(AttributeDef.class,attrMapping.getPointAttrName());
setCrs(ret,coordAttr);
CoordType coord=(CoordType) ( coordAttr.getDomainResolvingAliases());
ret.setDimension(coord.getDimensions().length);
setBB(ret, coord,attr.getContainer().getScopedName(null)+"."+attr.getName());
dbCol=ret;
Expand Down
1 change: 1 addition & 0 deletions src/ch/ehi/ili2db/fromili/TransferFromIli.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ private void generateItfLineTable(AttributeDef attr,int pass)
SurfaceOrAreaType type = (SurfaceOrAreaType)attr.getDomainResolvingAll();

DbColGeometry dbCol = recConv.generatePolylineType(type, attr.getContainer().getScopedName(null)+"."+attr.getName());
recConv.setCrs(dbCol, attr);
dbCol.setName(ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr,sqlName.getName()));
dbCol.setNotNull(true);
dbTable.addColumn(dbCol);
Expand Down
Loading

0 comments on commit 434307b

Please sign in to comment.