From f554ba0b3eec5cc79165c7930dfec558dc2559be Mon Sep 17 00:00:00 2001 From: "m.gond" Date: Tue, 15 Sep 2015 15:38:21 +0200 Subject: [PATCH] [extensions] Remove Healpix extensions from the Base sitools extensions (use fr.cnes.sitools.ext.astronomy) --- .../sitools/filter/healpix/HealpixFilter.java | 268 ---------------- .../resources/healpix/MocResource.java | 257 ---------------- .../resources/healpix/MocResourceModel.java | 90 ------ .../fr.cnes.sitools.filter.FilterHelper | 1 - ...s.sitools.plugins.resources.ResourceHelper | 1 - .../sitools/filter/healpix/HealpixFilter.java | 268 ---------------- .../resources/healpix/MocResource.java | 257 ---------------- .../resources/healpix/MocResourceModel.java | 90 ------ .../cnes/sitools/utils/wkt/TypeGeometry.java | 28 -- .../fr/cnes/sitools/utils/wkt/WKTReader.java | 290 ------------------ .../fr/cnes/sitools/utils/wkt/WKTWriter.java | 158 ---------- 11 files changed, 1708 deletions(-) delete mode 100644 workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java delete mode 100644 workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResource.java delete mode 100644 workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResource.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/TypeGeometry.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTReader.java delete mode 100644 workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTWriter.java diff --git a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java b/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java deleted file mode 100644 index 29b9b772..00000000 --- a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java +++ /dev/null @@ -1,268 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.filter.healpix; - -import java.util.*; - -import fr.cnes.sitools.common.validator.ConstraintViolation; -import fr.cnes.sitools.common.validator.ConstraintViolationLevel; -import fr.cnes.sitools.dataset.filter.business.AbstractFilter; -import fr.cnes.sitools.dataset.filter.model.FilterParameter; -import fr.cnes.sitools.dataset.filter.model.FilterParameterType; -import fr.cnes.sitools.dataset.model.geometry.*; -import fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter; -import fr.cnes.sitools.extensions.astro.application.opensearch.responsibility.SiaHealpix; -import fr.cnes.sitools.plugins.resources.model.ResourceModel; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.util.Util; -import fr.cnes.sitools.utils.wkt.WKTWriter; -import healpix.core.HealpixIndex; -import healpix.essentials.Pointing; -import healpix.essentials.Scheme; -import healpix.tools.SpatialVector; -import org.restlet.Request; -import org.restlet.data.Form; -import org.restlet.ext.wadl.ParameterInfo; -import org.restlet.ext.wadl.ParameterStyle; - -import fr.cnes.sitools.common.validator.Validator; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.dataset.model.Column; -import fr.cnes.sitools.dataset.model.DataSet; -import fr.cnes.sitools.dataset.model.Operator; -import fr.cnes.sitools.dataset.model.Predicat; - -/** - * Filter for Healpix parameters - *

- * Note : Create predicates only for postgis datasource - *

- * - * @author m.gond - */ -public class HealpixFilter extends AbstractFormFilter { - - private static final String ORDER = "order"; - private static final String HEALPIX = "healpix"; - private static final String COORD_SYSTEM = "coordSystem"; - - public static final String GEOMETRY_COLUMN = "geometryColumn"; - public static final String SRID = "srid"; - - // HEALPIX Properties - /** - * Multiplation factor to embed the entire Healpix pixel in the ROI. - */ - private static final double MULT_FACT = 1.5; - /** - * Max value in degree of latitude axis. - */ - private static final double MAX_DEC = 90.; - /** - * One degree. - */ - private static final double ONE_DEG = 1.0; - /** - * One degree in arsec. - */ - private static final double ONE_DEG_IN_ARSEC = 3600.; - /** - * Arcsec to degree conversion. - */ - private static final double ARCSEC2DEG = ONE_DEG / ONE_DEG_IN_ARSEC; - /** - * Size resolution. - */ - private transient double size; - /** - * Healpix index. - */ - private transient HealpixIndex index; - - /** - * BboxFilter constructor - */ - public HealpixFilter() { - super(); - this.setName("HealpixFilter"); - this.setDescription("Required when using Mizar component"); - this.setClassAuthor("AKKA Technologies"); - this.setClassOwner("CNES"); - this.setClassVersion("0.1"); - this.setDefaultFilter(false); - - FilterParameter param1 = new FilterParameter(GEOMETRY_COLUMN, "The name of column containing the geometry information", FilterParameterType.PARAMETER_IN); - param1.setValueType("xs:dataset.columnAlias"); - this.addParam(param1); - - FilterParameter srid = new FilterParameter(SRID, "The SRID number to use for postgis request", FilterParameterType.PARAMETER_INTERN); - srid.setValueType("xs:int"); - this.addParam(srid); - } - - /* - * (non-Javadoc) - * - * Parameters : - * order:9 - * healpix:1157742 - * coordSystem:EQUATORIAL - * - * - * @see fr.cnes.sitools.dataset.filter.business.AbstractFilter# getRequestParamsDescription() - */ - @Override - public HashMap getRequestParamsDescription() { - HashMap rpd = new HashMap(); - ParameterInfo paramInfo; - paramInfo = new ParameterInfo(ORDER, true, "xs:int", ParameterStyle.QUERY, "healpix order"); - rpd.put("0", paramInfo); - paramInfo = new ParameterInfo(HEALPIX, true, "xs:int", ParameterStyle.QUERY, "healpix index"); - rpd.put("1", paramInfo); - paramInfo = new ParameterInfo(COORD_SYSTEM, true, "xs:STRING", ParameterStyle.QUERY, "EQUATORIAL or GALACTIC"); - rpd.put("2", paramInfo); - this.setRequestParamsDescription(rpd); - return rpd; - } - - @Override - public List createPredicats(Request request, List predicats) throws Exception { - - DataSetApplication dsApplication = null; - DataSet ds = null; - - //lets apply our filter - if (dsApplication == null) { - dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication"); - ds = dsApplication.getDataSet(); - } - - Map filterParam = this.getParametersMap(); - FilterParameter paramGeoCol = filterParam.get(GEOMETRY_COLUMN); - FilterParameter paramSrid = filterParam.get(SRID); - - - Form params = request.getResourceRef().getQueryAsForm(); - - // Build predicat for filters param - String orderString = params.getFirstValue(ORDER); - String healpixString = params.getFirstValue(HEALPIX); - String coordSystem = params.getFirstValue(COORD_SYSTEM); - - if (checkParam(orderString, healpixString, coordSystem, paramGeoCol, paramSrid)) { - - String columnAlias = paramGeoCol.getAttachedColumn(); - String srid = paramSrid.getValue(); - - int order = Integer.parseInt(orderString); - long healpix = Long.parseLong(healpixString); - - Column col = ds.findByColumnAlias(columnAlias); - if (col != null && col.getFilter() != null && col.getFilter()) { - - //Calculer un polygon a partir des numeros Healpix - final int nside = (int) Math.pow(2, order); - index = new HealpixIndex(nside, Scheme.NESTED); - SpatialVector[] vectors = index.corners_nest(healpix, 2); - - String wkt = getWKTString(vectors); - - String predicatFormat = " AND st_intersects(%s, ST_GeomFromText('%s', %s))"; - String predicatString = String.format(predicatFormat, col.getDataIndex(), wkt, srid); - - Predicat pred = new Predicat(); - pred.setStringDefinition(predicatString); - predicats.add(pred); - } - } - return predicats; - } - - /** - * Generate a WKT string from the given Polygon identified by the array of SpatialVector - * @param vectors the list of vectors - * @return a WKT string - */ - private String getWKTString(SpatialVector[] vectors) { - Polygon geom = new Polygon(); - List points = new ArrayList(); - LngLatAlt firstPoint = null; - for (SpatialVector vector : vectors) { - double latitude = vector.ra(); - if (latitude > 180) { - latitude -= 360; - } - - if (firstPoint == null) { - firstPoint = new LngLatAlt(latitude, vector.dec()); - } - points.add(new LngLatAlt(latitude, vector.dec())); - } - points.add(firstPoint); - geom.add(points); - - return WKTWriter.write(geom); - } - - private boolean checkParam(String orderString, String healpixString, String coordSystem, FilterParameter columnAlias, FilterParameter srid) { - return !Util.isEmpty(orderString) && - !Util.isEmpty(healpixString) && - !Util.isEmpty(coordSystem) && - columnAlias != null && - !Util.isEmpty(columnAlias.getAttachedColumn()) && - srid != null && - !Util.isEmpty(srid.getValue()); - } - - @Override - public Validator getValidator() { - return new Validator() { - - @Override - public Set validate(AbstractFilter item) { - Set constraints = new HashSet(); - Map params = item.getParametersMap(); - - //GEOMETRY COLUMN - FilterParameter param = params.get(GEOMETRY_COLUMN); - String value = param.getAttachedColumn(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is not column defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - - //SRID - param = params.get(SRID); - value = param.getValue(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is srid defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - return constraints; - } - }; - } - -} diff --git a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResource.java b/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResource.java deleted file mode 100644 index 4625f631..00000000 --- a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResource.java +++ /dev/null @@ -1,257 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.resources.healpix; - -import cds.moc.HealpixMoc; -import cds.moc.MocCell; -import fr.cnes.sitools.common.exception.SitoolsException; -import fr.cnes.sitools.common.resource.SitoolsParameterizedResource; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.dataset.converter.business.ConverterChained; -import fr.cnes.sitools.dataset.database.DatabaseRequest; -import fr.cnes.sitools.dataset.database.DatabaseRequestFactory; -import fr.cnes.sitools.dataset.database.DatabaseRequestParameters; -import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil; -import fr.cnes.sitools.dataset.model.Column; -import fr.cnes.sitools.dataset.model.DataSet; -import fr.cnes.sitools.dataset.model.SpecificColumnType; -import fr.cnes.sitools.dataset.model.geometry.LngLatAlt; -import fr.cnes.sitools.dataset.model.geometry.Point; -import fr.cnes.sitools.dataset.model.geometry.Polygon; -import fr.cnes.sitools.datasource.jdbc.model.AttributeValue; -import fr.cnes.sitools.datasource.jdbc.model.Record; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.resources.geojson.GeoJSONPostGisResourceModel; -import fr.cnes.sitools.util.Util; -import fr.cnes.sitools.utils.wkt.WKTReader; -import healpix.core.HealpixIndex; -import healpix.core.base.set.LongRangeSet; -import healpix.essentials.Pointing; -import healpix.essentials.Scheme; -import healpix.essentials.Vec3; -import healpix.tools.SpatialVector; -import org.restlet.Context; -import org.restlet.data.MediaType; -import org.restlet.ext.wadl.MethodInfo; -import org.restlet.representation.Representation; -import org.restlet.representation.StringRepresentation; -import org.restlet.representation.Variant; -import org.restlet.resource.Get; - -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Classic implementation of JeoSearch resource - * - * - * @author m.gond - */ -public class MocResource extends SitoolsParameterizedResource { - - private static final String GEO_COLUMN_NAME = "sitools_geo_column"; - - private static final int DEFAULT_ORDER = 12; - - private int nside; - private int order; - - @Override - public void sitoolsDescribe() { - setName("MocResource"); - setDescription("MocResource"); - } - - - @Override - public void doInit() { - super.doInit(); - ResourceParameter geometryColumnParam = this.getModel().getParameterByName(MocResourceModel.ORDER_PARAM); - if(!Util.isEmpty(geometryColumnParam.getValue())) { - order = Integer.parseInt(geometryColumnParam.getValue()); - } - else { - order = DEFAULT_ORDER; - } - nside = (int) Math.pow(2, order); - } - - /** - * Get GeoJSON - * - * @return Representation the GeoJSON result - */ - @Get - public Representation get() { - Logger logger = getContext().getLogger(); - - // Get context - Context context = getContext(); - - // generate the DatabaseRequest - DataSetApplication datasetApp = (DataSetApplication) getApplication(); - DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext()); - - // Get request parameters - if (datasetApp.getConverterChained() != null) { - datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest()); - } - - // Get DatabaseRequestParameters - DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams(); - - ResourceParameter geometryColumnParam = this.getModel().getParameterByName(MocResourceModel.GEOMETRY_COLUMN); - String geometryColName = geometryColumnParam.getValue(); - - // modify the sql visible columns to return some geoJSON - List col = new ArrayList(params.getSqlVisibleColumns()); - List columnModel = params.getDataset().getColumnModel(); - for (Column column : columnModel) { - if (column.getColumnAlias().equals(geometryColName)) { - Column geoCol = new Column(); - // then add the column with the GeoJSON export - geoCol.setColumnAlias(GEO_COLUMN_NAME); - // Ne doit pas servir normalement - // geoCol.setColumnAliasDetail(column.getColumnAliasDetail()); - geoCol.setDataIndex("ST_AsText(" + column.getTableName() + "." + column.getDataIndex() + ")"); - geoCol.setSpecificColumnType(SpecificColumnType.SQL); - geoCol.setJavaSqlColumnType(column.getJavaSqlColumnType()); - geoCol.setVisible(true); - col.add(geoCol); - break; - } - } - params.setSqlVisibleColumns(col); - params.setPaginationExtend(-1); - - DataSet dataset = datasetApp.getDataSet(); - - DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params); - Record rec = null; - HealpixMoc moc = new HealpixMoc(); - try { - if (params.getDistinct()) { - databaseRequest.createDistinctRequest(); - } else { - databaseRequest.createRequest(); - } - - ConverterChained converterChained = datasetApp.getConverterChained(); - - HealpixIndex index = new HealpixIndex(nside, Scheme.NESTED); - - while (databaseRequest.nextResult()) { - - rec = databaseRequest.getRecord(); - if (Util.isSet(converterChained)) { - rec = converterChained.getConversionOf(rec); - } - - for (AttributeValue attr : rec.getAttributeValues()) { - if (attr.getName().equals(GEO_COLUMN_NAME)) { - //Parse WKT - String wkt = (String) attr.getValue(); - if (wkt.startsWith("POLYGON")) { - Polygon polygon = WKTReader.parsePolygon(wkt); - List coords = polygon.getExteriorRing(); - - ArrayList vectors = new ArrayList(); - - for (LngLatAlt coord : coords) { - SpatialVector spatialVector = new SpatialVector(); - double longitude = coord.getLongitude(); - if (longitude < 0) { - longitude += 360; - } - double lat = coord.getLatitude(); - spatialVector.set(longitude, lat); - vectors.add(spatialVector); - } - LongRangeSet rangeSet = index.query_polygon(nside, vectors, 1, 1); - - for (long pix : rangeSet) { - final MocCell mocCell = new MocCell(); - mocCell.set(order, pix); - moc.add(mocCell); - } - } - - if(wkt.startsWith("POINT")) { - - Point point = WKTReader.parsePoint(wkt); - double longitude = point.getLongitude(); - if (longitude < 0) { - longitude += 360; - } - double lat = point.getLatitude(); - - SpatialVector vec = new SpatialVector(); - vec.set(longitude, lat); - Pointing pointing = new Pointing(vec); - - long pix = index.ang2pix_nest(pointing.theta, pointing.phi); - final MocCell mocCell = new MocCell(); - mocCell.set(order, pix); - moc.add(mocCell); - } - break; - } - } - } - } catch (SitoolsException e) { - logger.log(Level.WARNING, e.getMessage(), e); - } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); - } finally { - if (databaseRequest != null) { - try { - databaseRequest.close(); - databaseRequest.close(); - } catch (SitoolsException e) { - context.getLogger().log(Level.SEVERE, "Cannot close database request", e); - } - } - } - moc.sort(); - return new StringRepresentation(moc.toString(), MediaType.APPLICATION_JSON); - } - - @Override - protected void describeGet(MethodInfo info) { - this.addInfo(info); - info.setIdentifier("retrieve_records and format it as GeoJSON"); - info.setDocumentation("Method to retrieve records of a dataset and format it as GeoJSON"); - addStandardGetRequestInfo(info); - DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info); - DataSetApplication application = (DataSetApplication) getApplication(); - DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained()); - addStandardResponseInfo(info); - addStandardInternalServerErrorInfo(info); - } - - @Override - protected Representation head(Variant variant) { - Representation repr = super.head(); - repr.setMediaType(MediaType.APPLICATION_JSON); - return repr; - } - -} diff --git a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java b/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java deleted file mode 100644 index cfb3c377..00000000 --- a/workspace/fr.cnes.sitools.ext.test/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java +++ /dev/null @@ -1,90 +0,0 @@ -package fr.cnes.sitools.resources.healpix; - -import fr.cnes.sitools.common.validator.ConstraintViolation; -import fr.cnes.sitools.common.validator.ConstraintViolationLevel; -import fr.cnes.sitools.common.validator.Validator; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.plugins.resources.model.DataSetSelectionType; -import fr.cnes.sitools.plugins.resources.model.ResourceModel; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.plugins.resources.model.ResourceParameterType; -import fr.cnes.sitools.util.Util; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class MocResourceModel extends ResourceModel { - - public static final String GEOMETRY_COLUMN = "geometryColumn"; - - public static final String ORDER_PARAM = "order"; - - - /** - * JeoSearchResourceModel constructor - */ - public MocResourceModel() { - super(); - setClassAuthor("AKKA Tecnologies"); - setClassOwner("CNES"); - setClassVersion("0.1"); - setName("MocResourceModel"); - setDescription("Generate MOC from dataset"); - setResourceClassName(MocResource.class.getName()); - - - ResourceParameter param1 = new ResourceParameter(GEOMETRY_COLUMN, "The name of column containing the geometry information", - ResourceParameterType.PARAMETER_INTERN); - param1.setValueType("xs:dataset.columnAlias"); - this.addParam(param1); - - ResourceParameter paramOrder = new ResourceParameter(ORDER_PARAM, "The order to generate the moc (default to 12)", - ResourceParameterType.PARAMETER_INTERN); - paramOrder.setValueType("xs:integer"); - this.addParam(paramOrder); - - this.setApplicationClassName(DataSetApplication.class.getName()); - this.setDataSetSelection(DataSetSelectionType.ALL); - this.getParameterByName("methods").setValue("GET"); - this.getParameterByName("url").setValue("/moc"); - - } - - @Override - public Validator getValidator() { - return new Validator() { - - @Override - public Set validate(ResourceModel item) { - Set constraints = new HashSet(); - Map params = item.getParametersMap(); - ResourceParameter param = params.get(GEOMETRY_COLUMN); - String value = param.getValue(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is not column defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - - param = params.get(ORDER_PARAM); - value = param.getValue(); - if (!Util.isEmpty(value)) { - try { - Integer.parseInt(value); - } catch (Exception e) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("Please enter an integer value"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - } - return constraints; - } - }; - } - -} diff --git a/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.filter.FilterHelper b/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.filter.FilterHelper index 0d972137..4b020fd5 100644 --- a/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.filter.FilterHelper +++ b/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.filter.FilterHelper @@ -1,3 +1,2 @@ fr.cnes.sitools.filter.basic.LoggingFilter fr.cnes.sitools.filter.basic.SecurityFilter -fr.cnes.sitools.filter.healpix.HealpixFilter diff --git a/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.plugins.resources.ResourceHelper b/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.plugins.resources.ResourceHelper index 87e9800f..53158c6f 100644 --- a/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.plugins.resources.ResourceHelper +++ b/workspace/fr.cnes.sitools.extensions/src/META-INF/services/fr.cnes.sitools.plugins.resources.ResourceHelper @@ -11,4 +11,3 @@ fr.cnes.sitools.resources.order.cart.streaming.StreamingOrderResourceModel fr.cnes.sitools.resources.order.cart.wget.WgetListFileOrderResourceModel fr.cnes.sitools.resources.order.cart.wget.WgetArchiveOrderResourceModel fr.cnes.sitools.resources.geojson.GeoJSONPostGisResourceModel -fr.cnes.sitools.resources.healpix.MocResourceModel \ No newline at end of file diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java deleted file mode 100644 index 29b9b772..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/filter/healpix/HealpixFilter.java +++ /dev/null @@ -1,268 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.filter.healpix; - -import java.util.*; - -import fr.cnes.sitools.common.validator.ConstraintViolation; -import fr.cnes.sitools.common.validator.ConstraintViolationLevel; -import fr.cnes.sitools.dataset.filter.business.AbstractFilter; -import fr.cnes.sitools.dataset.filter.model.FilterParameter; -import fr.cnes.sitools.dataset.filter.model.FilterParameterType; -import fr.cnes.sitools.dataset.model.geometry.*; -import fr.cnes.sitools.dataset.plugins.filters.core.AbstractFormFilter; -import fr.cnes.sitools.extensions.astro.application.opensearch.responsibility.SiaHealpix; -import fr.cnes.sitools.plugins.resources.model.ResourceModel; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.util.Util; -import fr.cnes.sitools.utils.wkt.WKTWriter; -import healpix.core.HealpixIndex; -import healpix.essentials.Pointing; -import healpix.essentials.Scheme; -import healpix.tools.SpatialVector; -import org.restlet.Request; -import org.restlet.data.Form; -import org.restlet.ext.wadl.ParameterInfo; -import org.restlet.ext.wadl.ParameterStyle; - -import fr.cnes.sitools.common.validator.Validator; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.dataset.model.Column; -import fr.cnes.sitools.dataset.model.DataSet; -import fr.cnes.sitools.dataset.model.Operator; -import fr.cnes.sitools.dataset.model.Predicat; - -/** - * Filter for Healpix parameters - *

- * Note : Create predicates only for postgis datasource - *

- * - * @author m.gond - */ -public class HealpixFilter extends AbstractFormFilter { - - private static final String ORDER = "order"; - private static final String HEALPIX = "healpix"; - private static final String COORD_SYSTEM = "coordSystem"; - - public static final String GEOMETRY_COLUMN = "geometryColumn"; - public static final String SRID = "srid"; - - // HEALPIX Properties - /** - * Multiplation factor to embed the entire Healpix pixel in the ROI. - */ - private static final double MULT_FACT = 1.5; - /** - * Max value in degree of latitude axis. - */ - private static final double MAX_DEC = 90.; - /** - * One degree. - */ - private static final double ONE_DEG = 1.0; - /** - * One degree in arsec. - */ - private static final double ONE_DEG_IN_ARSEC = 3600.; - /** - * Arcsec to degree conversion. - */ - private static final double ARCSEC2DEG = ONE_DEG / ONE_DEG_IN_ARSEC; - /** - * Size resolution. - */ - private transient double size; - /** - * Healpix index. - */ - private transient HealpixIndex index; - - /** - * BboxFilter constructor - */ - public HealpixFilter() { - super(); - this.setName("HealpixFilter"); - this.setDescription("Required when using Mizar component"); - this.setClassAuthor("AKKA Technologies"); - this.setClassOwner("CNES"); - this.setClassVersion("0.1"); - this.setDefaultFilter(false); - - FilterParameter param1 = new FilterParameter(GEOMETRY_COLUMN, "The name of column containing the geometry information", FilterParameterType.PARAMETER_IN); - param1.setValueType("xs:dataset.columnAlias"); - this.addParam(param1); - - FilterParameter srid = new FilterParameter(SRID, "The SRID number to use for postgis request", FilterParameterType.PARAMETER_INTERN); - srid.setValueType("xs:int"); - this.addParam(srid); - } - - /* - * (non-Javadoc) - * - * Parameters : - * order:9 - * healpix:1157742 - * coordSystem:EQUATORIAL - * - * - * @see fr.cnes.sitools.dataset.filter.business.AbstractFilter# getRequestParamsDescription() - */ - @Override - public HashMap getRequestParamsDescription() { - HashMap rpd = new HashMap(); - ParameterInfo paramInfo; - paramInfo = new ParameterInfo(ORDER, true, "xs:int", ParameterStyle.QUERY, "healpix order"); - rpd.put("0", paramInfo); - paramInfo = new ParameterInfo(HEALPIX, true, "xs:int", ParameterStyle.QUERY, "healpix index"); - rpd.put("1", paramInfo); - paramInfo = new ParameterInfo(COORD_SYSTEM, true, "xs:STRING", ParameterStyle.QUERY, "EQUATORIAL or GALACTIC"); - rpd.put("2", paramInfo); - this.setRequestParamsDescription(rpd); - return rpd; - } - - @Override - public List createPredicats(Request request, List predicats) throws Exception { - - DataSetApplication dsApplication = null; - DataSet ds = null; - - //lets apply our filter - if (dsApplication == null) { - dsApplication = (DataSetApplication) getContext().getAttributes().get("DataSetApplication"); - ds = dsApplication.getDataSet(); - } - - Map filterParam = this.getParametersMap(); - FilterParameter paramGeoCol = filterParam.get(GEOMETRY_COLUMN); - FilterParameter paramSrid = filterParam.get(SRID); - - - Form params = request.getResourceRef().getQueryAsForm(); - - // Build predicat for filters param - String orderString = params.getFirstValue(ORDER); - String healpixString = params.getFirstValue(HEALPIX); - String coordSystem = params.getFirstValue(COORD_SYSTEM); - - if (checkParam(orderString, healpixString, coordSystem, paramGeoCol, paramSrid)) { - - String columnAlias = paramGeoCol.getAttachedColumn(); - String srid = paramSrid.getValue(); - - int order = Integer.parseInt(orderString); - long healpix = Long.parseLong(healpixString); - - Column col = ds.findByColumnAlias(columnAlias); - if (col != null && col.getFilter() != null && col.getFilter()) { - - //Calculer un polygon a partir des numeros Healpix - final int nside = (int) Math.pow(2, order); - index = new HealpixIndex(nside, Scheme.NESTED); - SpatialVector[] vectors = index.corners_nest(healpix, 2); - - String wkt = getWKTString(vectors); - - String predicatFormat = " AND st_intersects(%s, ST_GeomFromText('%s', %s))"; - String predicatString = String.format(predicatFormat, col.getDataIndex(), wkt, srid); - - Predicat pred = new Predicat(); - pred.setStringDefinition(predicatString); - predicats.add(pred); - } - } - return predicats; - } - - /** - * Generate a WKT string from the given Polygon identified by the array of SpatialVector - * @param vectors the list of vectors - * @return a WKT string - */ - private String getWKTString(SpatialVector[] vectors) { - Polygon geom = new Polygon(); - List points = new ArrayList(); - LngLatAlt firstPoint = null; - for (SpatialVector vector : vectors) { - double latitude = vector.ra(); - if (latitude > 180) { - latitude -= 360; - } - - if (firstPoint == null) { - firstPoint = new LngLatAlt(latitude, vector.dec()); - } - points.add(new LngLatAlt(latitude, vector.dec())); - } - points.add(firstPoint); - geom.add(points); - - return WKTWriter.write(geom); - } - - private boolean checkParam(String orderString, String healpixString, String coordSystem, FilterParameter columnAlias, FilterParameter srid) { - return !Util.isEmpty(orderString) && - !Util.isEmpty(healpixString) && - !Util.isEmpty(coordSystem) && - columnAlias != null && - !Util.isEmpty(columnAlias.getAttachedColumn()) && - srid != null && - !Util.isEmpty(srid.getValue()); - } - - @Override - public Validator getValidator() { - return new Validator() { - - @Override - public Set validate(AbstractFilter item) { - Set constraints = new HashSet(); - Map params = item.getParametersMap(); - - //GEOMETRY COLUMN - FilterParameter param = params.get(GEOMETRY_COLUMN); - String value = param.getAttachedColumn(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is not column defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - - //SRID - param = params.get(SRID); - value = param.getValue(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is srid defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - return constraints; - } - }; - } - -} diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResource.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResource.java deleted file mode 100644 index 4625f631..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResource.java +++ /dev/null @@ -1,257 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.resources.healpix; - -import cds.moc.HealpixMoc; -import cds.moc.MocCell; -import fr.cnes.sitools.common.exception.SitoolsException; -import fr.cnes.sitools.common.resource.SitoolsParameterizedResource; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.dataset.converter.business.ConverterChained; -import fr.cnes.sitools.dataset.database.DatabaseRequest; -import fr.cnes.sitools.dataset.database.DatabaseRequestFactory; -import fr.cnes.sitools.dataset.database.DatabaseRequestParameters; -import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil; -import fr.cnes.sitools.dataset.model.Column; -import fr.cnes.sitools.dataset.model.DataSet; -import fr.cnes.sitools.dataset.model.SpecificColumnType; -import fr.cnes.sitools.dataset.model.geometry.LngLatAlt; -import fr.cnes.sitools.dataset.model.geometry.Point; -import fr.cnes.sitools.dataset.model.geometry.Polygon; -import fr.cnes.sitools.datasource.jdbc.model.AttributeValue; -import fr.cnes.sitools.datasource.jdbc.model.Record; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.resources.geojson.GeoJSONPostGisResourceModel; -import fr.cnes.sitools.util.Util; -import fr.cnes.sitools.utils.wkt.WKTReader; -import healpix.core.HealpixIndex; -import healpix.core.base.set.LongRangeSet; -import healpix.essentials.Pointing; -import healpix.essentials.Scheme; -import healpix.essentials.Vec3; -import healpix.tools.SpatialVector; -import org.restlet.Context; -import org.restlet.data.MediaType; -import org.restlet.ext.wadl.MethodInfo; -import org.restlet.representation.Representation; -import org.restlet.representation.StringRepresentation; -import org.restlet.representation.Variant; -import org.restlet.resource.Get; - -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Classic implementation of JeoSearch resource - * - * - * @author m.gond - */ -public class MocResource extends SitoolsParameterizedResource { - - private static final String GEO_COLUMN_NAME = "sitools_geo_column"; - - private static final int DEFAULT_ORDER = 12; - - private int nside; - private int order; - - @Override - public void sitoolsDescribe() { - setName("MocResource"); - setDescription("MocResource"); - } - - - @Override - public void doInit() { - super.doInit(); - ResourceParameter geometryColumnParam = this.getModel().getParameterByName(MocResourceModel.ORDER_PARAM); - if(!Util.isEmpty(geometryColumnParam.getValue())) { - order = Integer.parseInt(geometryColumnParam.getValue()); - } - else { - order = DEFAULT_ORDER; - } - nside = (int) Math.pow(2, order); - } - - /** - * Get GeoJSON - * - * @return Representation the GeoJSON result - */ - @Get - public Representation get() { - Logger logger = getContext().getLogger(); - - // Get context - Context context = getContext(); - - // generate the DatabaseRequest - DataSetApplication datasetApp = (DataSetApplication) getApplication(); - DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext()); - - // Get request parameters - if (datasetApp.getConverterChained() != null) { - datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest()); - } - - // Get DatabaseRequestParameters - DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams(); - - ResourceParameter geometryColumnParam = this.getModel().getParameterByName(MocResourceModel.GEOMETRY_COLUMN); - String geometryColName = geometryColumnParam.getValue(); - - // modify the sql visible columns to return some geoJSON - List col = new ArrayList(params.getSqlVisibleColumns()); - List columnModel = params.getDataset().getColumnModel(); - for (Column column : columnModel) { - if (column.getColumnAlias().equals(geometryColName)) { - Column geoCol = new Column(); - // then add the column with the GeoJSON export - geoCol.setColumnAlias(GEO_COLUMN_NAME); - // Ne doit pas servir normalement - // geoCol.setColumnAliasDetail(column.getColumnAliasDetail()); - geoCol.setDataIndex("ST_AsText(" + column.getTableName() + "." + column.getDataIndex() + ")"); - geoCol.setSpecificColumnType(SpecificColumnType.SQL); - geoCol.setJavaSqlColumnType(column.getJavaSqlColumnType()); - geoCol.setVisible(true); - col.add(geoCol); - break; - } - } - params.setSqlVisibleColumns(col); - params.setPaginationExtend(-1); - - DataSet dataset = datasetApp.getDataSet(); - - DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params); - Record rec = null; - HealpixMoc moc = new HealpixMoc(); - try { - if (params.getDistinct()) { - databaseRequest.createDistinctRequest(); - } else { - databaseRequest.createRequest(); - } - - ConverterChained converterChained = datasetApp.getConverterChained(); - - HealpixIndex index = new HealpixIndex(nside, Scheme.NESTED); - - while (databaseRequest.nextResult()) { - - rec = databaseRequest.getRecord(); - if (Util.isSet(converterChained)) { - rec = converterChained.getConversionOf(rec); - } - - for (AttributeValue attr : rec.getAttributeValues()) { - if (attr.getName().equals(GEO_COLUMN_NAME)) { - //Parse WKT - String wkt = (String) attr.getValue(); - if (wkt.startsWith("POLYGON")) { - Polygon polygon = WKTReader.parsePolygon(wkt); - List coords = polygon.getExteriorRing(); - - ArrayList vectors = new ArrayList(); - - for (LngLatAlt coord : coords) { - SpatialVector spatialVector = new SpatialVector(); - double longitude = coord.getLongitude(); - if (longitude < 0) { - longitude += 360; - } - double lat = coord.getLatitude(); - spatialVector.set(longitude, lat); - vectors.add(spatialVector); - } - LongRangeSet rangeSet = index.query_polygon(nside, vectors, 1, 1); - - for (long pix : rangeSet) { - final MocCell mocCell = new MocCell(); - mocCell.set(order, pix); - moc.add(mocCell); - } - } - - if(wkt.startsWith("POINT")) { - - Point point = WKTReader.parsePoint(wkt); - double longitude = point.getLongitude(); - if (longitude < 0) { - longitude += 360; - } - double lat = point.getLatitude(); - - SpatialVector vec = new SpatialVector(); - vec.set(longitude, lat); - Pointing pointing = new Pointing(vec); - - long pix = index.ang2pix_nest(pointing.theta, pointing.phi); - final MocCell mocCell = new MocCell(); - mocCell.set(order, pix); - moc.add(mocCell); - } - break; - } - } - } - } catch (SitoolsException e) { - logger.log(Level.WARNING, e.getMessage(), e); - } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); - } finally { - if (databaseRequest != null) { - try { - databaseRequest.close(); - databaseRequest.close(); - } catch (SitoolsException e) { - context.getLogger().log(Level.SEVERE, "Cannot close database request", e); - } - } - } - moc.sort(); - return new StringRepresentation(moc.toString(), MediaType.APPLICATION_JSON); - } - - @Override - protected void describeGet(MethodInfo info) { - this.addInfo(info); - info.setIdentifier("retrieve_records and format it as GeoJSON"); - info.setDocumentation("Method to retrieve records of a dataset and format it as GeoJSON"); - addStandardGetRequestInfo(info); - DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info); - DataSetApplication application = (DataSetApplication) getApplication(); - DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained()); - addStandardResponseInfo(info); - addStandardInternalServerErrorInfo(info); - } - - @Override - protected Representation head(Variant variant) { - Representation repr = super.head(); - repr.setMediaType(MediaType.APPLICATION_JSON); - return repr; - } - -} diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java deleted file mode 100644 index cfb3c377..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/resources/healpix/MocResourceModel.java +++ /dev/null @@ -1,90 +0,0 @@ -package fr.cnes.sitools.resources.healpix; - -import fr.cnes.sitools.common.validator.ConstraintViolation; -import fr.cnes.sitools.common.validator.ConstraintViolationLevel; -import fr.cnes.sitools.common.validator.Validator; -import fr.cnes.sitools.dataset.DataSetApplication; -import fr.cnes.sitools.plugins.resources.model.DataSetSelectionType; -import fr.cnes.sitools.plugins.resources.model.ResourceModel; -import fr.cnes.sitools.plugins.resources.model.ResourceParameter; -import fr.cnes.sitools.plugins.resources.model.ResourceParameterType; -import fr.cnes.sitools.util.Util; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class MocResourceModel extends ResourceModel { - - public static final String GEOMETRY_COLUMN = "geometryColumn"; - - public static final String ORDER_PARAM = "order"; - - - /** - * JeoSearchResourceModel constructor - */ - public MocResourceModel() { - super(); - setClassAuthor("AKKA Tecnologies"); - setClassOwner("CNES"); - setClassVersion("0.1"); - setName("MocResourceModel"); - setDescription("Generate MOC from dataset"); - setResourceClassName(MocResource.class.getName()); - - - ResourceParameter param1 = new ResourceParameter(GEOMETRY_COLUMN, "The name of column containing the geometry information", - ResourceParameterType.PARAMETER_INTERN); - param1.setValueType("xs:dataset.columnAlias"); - this.addParam(param1); - - ResourceParameter paramOrder = new ResourceParameter(ORDER_PARAM, "The order to generate the moc (default to 12)", - ResourceParameterType.PARAMETER_INTERN); - paramOrder.setValueType("xs:integer"); - this.addParam(paramOrder); - - this.setApplicationClassName(DataSetApplication.class.getName()); - this.setDataSetSelection(DataSetSelectionType.ALL); - this.getParameterByName("methods").setValue("GET"); - this.getParameterByName("url").setValue("/moc"); - - } - - @Override - public Validator getValidator() { - return new Validator() { - - @Override - public Set validate(ResourceModel item) { - Set constraints = new HashSet(); - Map params = item.getParametersMap(); - ResourceParameter param = params.get(GEOMETRY_COLUMN); - String value = param.getValue(); - if (value.equals("")) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("There is not column defined"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - - param = params.get(ORDER_PARAM); - value = param.getValue(); - if (!Util.isEmpty(value)) { - try { - Integer.parseInt(value); - } catch (Exception e) { - ConstraintViolation constraint = new ConstraintViolation(); - constraint.setMessage("Please enter an integer value"); - constraint.setLevel(ConstraintViolationLevel.CRITICAL); - constraint.setValueName(param.getName()); - constraints.add(constraint); - } - } - return constraints; - } - }; - } - -} diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/TypeGeometry.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/TypeGeometry.java deleted file mode 100644 index ee6ecfe3..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/TypeGeometry.java +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.utils.wkt; - -public enum TypeGeometry { - POINT, - LINESTRING, - POLYGON, - MULTIPOINT, - MULTILINESTRING, - MULTIPOLYGON; -} diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTReader.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTReader.java deleted file mode 100644 index 73ea1e3f..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTReader.java +++ /dev/null @@ -1,290 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.utils.wkt; - - -import fr.cnes.sitools.dataset.model.geometry.LngLatAlt; -import fr.cnes.sitools.dataset.model.geometry.Point; -import fr.cnes.sitools.dataset.model.geometry.Polygon; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class WKTReader { - - /** - * Parse WKT POINT geometry - * - * @param wkt Expected format is "POINT(longitude latitude)" expressed in WSG84 - * @return Point object if wkt is well formed or null otherwise - */ - public static Point parsePoint(String wkt) { - if (wkt != null) { - String str = extractFromGeometry(TypeGeometry.POINT, wkt); - - List points = getLngLats(str); - if (points.size() > 0) { - return new Point(points.get(0)); - } - } - return null; - } - -// /** -// * Parse WKT MULTIPOINT geometry -// * -// * @param wkt Expected format is "MULTIPOINT ((lon lat), (lon lat), ... )" expressed in WSG84 -// * @return List of Point object if wkt is well formed or null otherwise -// */ -// public static MultiPoint parseMultiPoint(String wkt) { -// if (wkt != null) { -// String str = extractFromGeometry(TypeGeometry.MULTIPOINT, wkt); -// String[] pointsStr = str.split(","); -// -// MultiPoint multiPoint = new MultiPoint(); -// -// for (String aPointsStr : pointsStr) { -// Point p = parsePoint(aPointsStr); -// if (p != null) { -// multiPoint.add(p.getCoordinates()); -// } -// } -// return multiPoint; -// } -// return null; -// } -// -// /** -// * @param wkt Expected format is "LINESTRING (lon lat, lon lat, ..)" expressed in WSG84 -// * @return LineString object if wkt is well formed or null otherwise -// */ -// public static LineString parseLineString(String wkt) { -// if (wkt != null) { -// String str = extractFromGeometry(TypeGeometry.LINESTRING, wkt); -// -// return getLineString(str); -// } -// return null; -// } -// -// /** -// * @param wkt Expected format is "MULTILINESTRING ((lon lat, lon lat, ..), (lon lat, lon lat, ..), ...)" expressed in WSG84 -// * @return List of LineString object if wkt is well formed or null otherwise -// */ -// public static MultiLineString parseMultiLineString(String wkt) { -// if (wkt != null) { -// String str = extractFromGeometry(TypeGeometry.MULTILINESTRING, wkt); -// -// MultiLineString multiLineString = new MultiLineString(); -// -// String[] linesStr = extractFromList(str); -// -// for (String aLinesStr : linesStr) { -// LineString line = getLineString(aLinesStr); -// if (line != null) { -// multiLineString.add(line.getCoordinates()); -// } -// } -// -// return multiLineString; -// } -// return null; -// } - - /** - * @param wkt Expected format is "POLYGON ((lon lat, lon lat, ..)) or POLYGON ((lon lat, lon lat, ..), (lon lat, lon lat, ..))" - * expressed in WSG84 - * @return Polygon object if wkt is well formed or null otherwise - */ - public static Polygon parsePolygon(String wkt) { - if (wkt != null) { - String str = extractFromGeometry(TypeGeometry.POLYGON, wkt); - - return getPolygon(str); - } - return null; - } - - -// /** -// * @param wkt Expected format is "MULTIPOLYGON (((lon lat, lon lat, ..)), ((lon lat, lon lat, ..)), ...) -// * or MULTIPOLYGON (((lon lat, lon lat, ..), (lon lat, lon lat, ..))), ... )" -// * expressed in WSG84 -// * @return Polygon object if wkt is well formed or null otherwise -// */ -// public static MultiPolygon parseMultiPolygon(String wkt) { -// if (wkt != null) { -// String str = extractFromGeometry(TypeGeometry.MULTIPOLYGON, wkt); -// -// String[] polyStr = extractFromDoubleList(str); -// -// MultiPolygon multiPolygon = new MultiPolygon(); -// for (String aPolyStr : polyStr) { -// Polygon polygon; -// if (aPolyStr.contains(")")) { -// // Case of Polygon with interior and exterior ring -// polygon = parsePolygon(aPolyStr); -// } else { -// // Case of Polygon with only exterior ring -// polygon = new Polygon(getLngLats(aPolyStr)); -// } -// if (polygon != null) { -// multiPolygon.add(polygon); -// } -// } -// return multiPolygon; -// } -// return null; -// } - - /** - * @param str String with the following format : "lon lat, lon lat, ..." - * @return List of LngLatAlt object resulting of the parsing process - */ - private static List getLngLats(String str) { - - List points = new ArrayList(); - - Pattern pattern = Pattern.compile("(?:([-]?\\d+\\.?\\d*) \\s*([-]?\\d+\\.?\\d*))"); - Matcher matcher = pattern.matcher(str); - while (matcher.find()) { - double longitude, latitude; - try { - longitude = Double.parseDouble(matcher.group(1)); - latitude = Double.parseDouble(matcher.group(2)); - } catch (NumberFormatException ex) { - // Malformed number inside - return null; - } - points.add(new LngLatAlt(longitude, latitude)); - } - - return points; - } - -// /** -// * @param str String with the following format : "lon lat, lon lat, ..." -// * @return LineString object if wkt is well formed or null otherwise -// */ -// private static LineString getLineString(String str) { -// List points = getLngLats(str); -// -// if (points != null) { -// LineString lineString = new LineString(); -// -// for (LngLatAlt point : points) { -// lineString.add(point); -// } -// return lineString; -// } -// return null; -// } - - /** - * @param str String with the following format : "(lon lat, lon lat, ..)" or "(lon lat, lon lat, ..), (lon lat, lon lat, ..)" - * @return Polygon object if wkt is well formed or null otherwisePolygon object if wkt is well formed or null otherwise - */ - private static Polygon getPolygon(String str) { - String[] rings = extractFromList(str); - - assert rings.length > 0; - rings[0] = rings[0].replaceAll("\\(", "").trim(); - List pointsExterior = getLngLats(rings[0]); - - if (pointsExterior != null) { - Polygon polygon = new Polygon(pointsExterior); - - if (rings.length > 1) { - rings[1] = rings[1].replaceAll("\\(\\)", "").trim(); - List pointsInterior = getLngLats(rings[1]); - - polygon.addInteriorRing(pointsInterior); - } - - return polygon; - } - return null; - } - - /** - * If wkt has the following form : "TYPE ( ... ) or ( ... )", Then return the content "..." - * Otherwise it returns wkt string trimed and uppercased. - * - * @param type WKT Geometry type - * @param wkt WKT String - * @return Remove the "TYPE ( ... )" or enclosing parenthesis. - */ - private static String extractFromGeometry(TypeGeometry type, String wkt) { - - // Normalize the WKT string - wkt = wkt.trim().toUpperCase(); - - if (type != null) { - - Pattern pattern = Pattern.compile("(?:" + type.toString().toUpperCase() + ")?\\s*\\((.*)\\)"); - Matcher matcher = pattern.matcher(wkt); - if (matcher.find()) { - return matcher.group(1); - } - } - - return wkt; - } - - /** - * @param str String with the following format : "(lon lat, lon lat, ..)" or "(lon lat, lon lat, ..), (lon lat, lon lat, ..)" - * @return array of String with the following format : "lon lat, lon lat, .." - */ - private static String[] extractFromList(String str) { - - String[] results = str.split("\\)(\\s*,\\s*)?"); - - // Remove all remaining leading '(' character from the results - for (int i = 0; i < results.length; i++) { - results[i] = results[i].replaceAll("\\(", "").trim(); - } - - return results; - } - - /** - * @param str String with the following format : "((lon lat, lon lat, ..)") or "(lon lat, lon lat, ..), (lon lat, lon lat, ..)" - * @return array of String with the following format : "lon lat, lon lat, .." - */ - private static String[] extractFromDoubleList(String str) { - - String[] results = str.split("\\)\\)\\s*,\\s*"); - - // Remove all remaining leading '(' character from the results - for (int i = 0; i < results.length; i++) { - - if (!results[i].contains(")")) { - // Case of Polygon with only exterior ring, we have to remove the 2 leading "(" - results[i] = results[i].replaceAll("\\(", "").trim(); - } - //else : Case of Polygon with interior and exterior rings : we keep the current result as - // "((lon lat, lon lat, ..), (lon lat, lon lat, ..))" - } - - return results; - } -} - diff --git a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTWriter.java b/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTWriter.java deleted file mode 100644 index a681d46a..00000000 --- a/workspace/fr.cnes.sitools.extensions/src/fr/cnes/sitools/utils/wkt/WKTWriter.java +++ /dev/null @@ -1,158 +0,0 @@ -/******************************************************************************* - * Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES - * - * This file is part of SITools2. - * - * SITools2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * SITools2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with SITools2. If not, see . - ******************************************************************************/ -package fr.cnes.sitools.utils.wkt; - - -import fr.cnes.sitools.dataset.model.geometry.LngLatAlt; -import fr.cnes.sitools.dataset.model.geometry.Point; -import fr.cnes.sitools.dataset.model.geometry.Polygon; - -import java.util.List; - -public class WKTWriter { - - public static String write(Point point) { - if (point != null) { - return "POINT(" + point.getLongitude() + " " + point.getLatitude() + ")"; - } - return null; - } - -// public static String write(MultiPoint multiPoint) { -// if (multiPoint != null) { -// List points = multiPoint.getCoordinates(); -// if (points != null && points.size() > 0) { -// String wkt = "MULTIPOINT("; -// boolean first = true; -// for (LngLatAlt coords : points) { -// if (first) { -// first = false; -// } else { -// wkt += ","; -// } -// wkt += "(" + coords.getLongitude() + " " + coords.getLatitude() + ")"; -// } -// wkt += ")"; -// return wkt; -// } -// } -// return null; -// } -// -// -// public static String write(LineString lineString) { -// if (lineString != null) { -// List points = lineString.getCoordinates(); -// if (points != null && points.size() > 0) { -// return "LINESTRING(" + toWKT(points) + ")"; -// } -// } -// return null; -// } -// -// -// public static String write(MultiLineString multiLineString) { -// if (multiLineString != null) { -// List> lines = multiLineString.getCoordinates(); -// if (lines != null && lines.size() > 0) { -// String wkt = "MULTILINESTRING("; -// boolean first = true; -// for (List line : lines) { -// if (first) { -// first = false; -// } else { -// wkt += ","; -// } -// wkt += "(" + toWKT(line) + ")"; -// } -// wkt += ")"; -// return wkt; -// } -// } -// return null; -// } - - public static String write(Polygon polygon) { - if (polygon != null) { - List exteriorRing = polygon.getExteriorRing(); - if (exteriorRing != null) { - String wkt = "POLYGON("; - wkt += "(" + toWKT(exteriorRing) + ")"; - List> holes = polygon.getInteriorRings(); - if (holes != null && holes.size() > 0) { - for (List hole : holes) { - wkt += ",(" + toWKT(hole) + ")"; - } - } - wkt += ")"; - return wkt; - } - } - return null; - } - -// public static String write(MultiPolygon multiPolygon) { -// if (multiPolygon != null) { -// List>> polygons = multiPolygon.getCoordinates(); -// if (polygons != null && polygons.size() > 0) { -// String wkt = "MULTIPOLYGON("; -// boolean firstPolygon = true; -// for (List> polygon : polygons) { -// if (firstPolygon) { -// firstPolygon = false; -// wkt += "("; -// } else { -// wkt += ",("; -// } -// boolean firstRing = true; -// for (List ring : polygon) { -// if (firstRing) { -// firstRing = false; -// } else { -// wkt += ","; -// } -// wkt += "(" + toWKT(ring) + ")"; -// } -// wkt += ")"; -// } -// wkt += ")"; -// return wkt; -// } -// } -// return null; -// } - - /** - * @param points List of points - * @return Return String with the following format : "lon lat, lon lat, ..." - */ - private static String toWKT(List points) { - String wkt = ""; - boolean first = true; - for (LngLatAlt coords : points) { - if (first) { - first = false; - } else { - wkt += ","; - } - wkt += coords.getLongitude() + " " + coords.getLatitude(); - } - return wkt; - } -}