Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java @ 456:b8080695ea62
Splitted code pathes of dynamic and static nodes. Static nodes contain the selected value only instead of transfering the whole stack of options again.
gnv-artifacts/trunk@506 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 04 Jan 2010 15:41:29 +0000 |
parents | 363236fc462d |
children | 912797826099 |
line wrap: on
line source
/** * */ package de.intevation.gnv.state.profile.verticalcrosssection; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.awt.Dimension; import org.apache.log4j.Logger; import org.jfree.chart.ChartTheme; import net.sf.ehcache.Element; import au.com.bytecode.opencsv.CSVWriter; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.geom.Coordinate; import de.intevation.artifacts.CallContext; import de.intevation.gnv.artifacts.context.GNVArtifactContext; import de.intevation.gnv.artifacts.cache.CacheFactory; import de.intevation.gnv.chart.Chart; import de.intevation.gnv.chart.ChartLabels; import de.intevation.gnv.chart.ChartStyle; import de.intevation.gnv.chart.VerticalCrossSectionChart; import de.intevation.gnv.chart.exception.TechnicalChartException; import de.intevation.gnv.geobackend.base.Result; import de.intevation.gnv.geobackend.base.ResultDescriptor; import de.intevation.gnv.geobackend.base.query.QueryExecutor; import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; import de.intevation.gnv.geobackend.base.query.exception.QueryException; import de.intevation.gnv.math.AttributedXYColumns; import de.intevation.gnv.math.HeightValue; import de.intevation.gnv.math.QueriedXYDepth; import de.intevation.gnv.math.XYColumn; import de.intevation.gnv.math.IJKey; import de.intevation.gnv.math.LinearMetrics; import de.intevation.gnv.math.Interpolation3D; import de.intevation.gnv.math.ConstantXYDepth; import de.intevation.gnv.state.InputData; import de.intevation.gnv.state.describedata.KeyValueDescibeData; import de.intevation.gnv.state.exception.StateException; import de.intevation.gnv.state.timeseries.TimeSeriesOutputState; import de.intevation.gnv.statistics.Statistics; import de.intevation.gnv.statistics.VerticalCrossSectionStatistics; import de.intevation.gnv.utils.DistanceCalculator; import de.intevation.gnv.utils.WKTUtils; import de.intevation.gnv.utils.StringUtils; import de.intevation.gnv.raster.Filter; import de.intevation.gnv.raster.PaletteManager; import de.intevation.gnv.raster.Palette; import de.intevation.gnv.raster.Raster; import de.intevation.gnv.raster.PolygonDatasetProducer; import de.intevation.gnv.raster.IsoAttributeGenerator; import de.intevation.gnv.raster.IsoPolygonSeriesProducer; import de.intevation.gnv.raster.Vectorizer; import de.intevation.gnv.jfreechart.PolygonDataset; import de.intevation.gnv.jfreechart.PolygonSeries; /** * @author Tim Englich (tim.englich@intevation.de) * @author Ingo Weinzierl (iweinzierl@intevation.de) * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) */ public class VerticalCrossSectionOutputState extends TimeSeriesOutputState { public static final String[] ATTRIBUTE_LIST = { "SHAPE", "Z", "YORDINATE", "IPOSITION", "JPOSITION", "KPOSITION" }; private static Logger log = Logger.getLogger( VerticalCrossSectionOutputState.class); private String ijkQueryID = "horizontalprofile_meshpoint_cross_ij"; /** * The UID of this Class */ private static final long serialVersionUID = 3233620652465061860L; /** * Constructor */ public VerticalCrossSectionOutputState() { super(); super.domainLable = "chart.verticalcrosssection.title.xaxis"; } @Override protected Object getChartResult(String uuid, CallContext callContext) { log.debug("VerticalCrossSectionOutputState.getChartResult"); Collection<Result> result = null; String key = uuid + super.getID(); Element element = CacheFactory.getInstance().getCache().get(key); if (element != null) return element.getObjectValue(); log.debug("No results in cache yet."); InputData meshLine = inputData.get("mesh_linestring"); InputData meshId = inputData.get("meshid"); if (meshLine == null) { log.error("mesh_linestring is not defined"); throw new IllegalStateException("missing mesh_linestring"); } if (meshId == null) { log.error("meshid is not defined"); throw new IllegalStateException("missing meshid"); } Coordinate [] coords = WKTUtils.toCoordinates( meshLine.getValue()); if (coords == null) { throw new IllegalStateException("cannot read coordinates"); } try { String additionWhere = WKTUtils.worldCoordinatesToIndex( coords, result, meshId.getValue(), ijkQueryID); String[] addedFilterValues = StringUtils.append( generateFilterValuesFromInputData(), additionWhere); QueryExecutor exec = QueryExecutorFactory .getInstance() .getQueryExecutor(); result = exec.executeQuery(queryID, addedFilterValues); } catch (QueryException qe) { log.error(qe, qe); } Object obj = process( Arrays.asList(coords), preProcess(result), callContext); CacheFactory.getInstance().getCache().put(new Element(key, obj)); return obj; } private static Dimension getRasterSize(CallContext callContext) { GNVArtifactContext context = (GNVArtifactContext)callContext.globalContext(); Dimension size = (Dimension)context.get( GNVArtifactContext.VERTICAL_CROSS_SECTION_SAMPLES_KEY); return size != null ? size : GNVArtifactContext.DEFAULT_VERTICAL_CROSS_SECTION_SAMPLES; } private static List<Filter.Factory> getFilterFactories( CallContext callContext ) { GNVArtifactContext context = (GNVArtifactContext)callContext.globalContext(); List<Filter.Factory> factories = (List<Filter.Factory>)context.get( GNVArtifactContext.VERTICAL_CROSS_SECTION_FILTER_FACTORIES_KEY); return factories != null ? factories : new ArrayList<Filter.Factory>(); } private static Map<Integer, PaletteManager> getPalettes( CallContext callContext ) { GNVArtifactContext context = (GNVArtifactContext)callContext.globalContext(); Map<Integer, PaletteManager> palettes = (Map<Integer, PaletteManager>)context.get( GNVArtifactContext.PALETTES_KEY); return palettes != null ? palettes : new HashMap<Integer, PaletteManager>(); } public static final double EPSILON = 1e-5d; protected Object process( List<Coordinate> path, AttributedXYColumns columns, CallContext callContext ) { Integer parameterId = (Integer)columns.getAttribute("GROUP1"); // XXX: hardcoded if (parameterId == null) { log.error("missing parameter id"); return null; } Map<Integer, PaletteManager> paletteManagers = getPalettes(callContext); PaletteManager paletteManager = paletteManagers.get(parameterId); if (paletteManager == null) { log.error("no palette found for parameter id " + parameterId); return null; } boolean debug = log.isDebugEnabled(); if (debug) { log.debug("using palette '" + paletteManager.getName() + "'"); } Dimension rasterSize = getRasterSize(callContext); List<Filter.Factory> filterFactories = getFilterFactories(callContext); Interpolation3D interpolation = new Interpolation3D(rasterSize); double distance = DistanceCalculator.calculateDistance(path); if (distance < EPSILON) { log.warn("distance too short for interpolation"); return null; } boolean success = interpolation.interpolate( path, columns.getXYColumns(), 0d, distance, LinearMetrics.INSTANCE, new QueriedXYDepth()); //new ConstantXYDepth(-75d)); // TODO: Use DEM here!! if (!success) { log.warn("interpolation failed"); return null; } // Do the post processing Raster raster = new Raster( interpolation.getRaster(), rasterSize.width); for (Filter.Factory factory: filterFactories) { raster = factory.create().filter(raster); } if (debug) { log.debug("to indexed raster"); } // scan for regions with base palette Palette basePalette = paletteManager.getBase(); int [] intRaster = raster.toIndexed(basePalette); // produce JFreeChart compatible polygons if (debug) { log.debug("vectorize indexed raster"); } double maxDepth = interpolation.getMaxDepth(); PolygonDatasetProducer pdsp = new PolygonDatasetProducer( 0, 0, distance, maxDepth); int numRegions = new Vectorizer(intRaster, rasterSize.width) .process(pdsp); PolygonDataset pds = pdsp.getPolygonDataset(); int numColors = pds.getSeriesCount(); if (debug) { log.debug("number of regions: " + numRegions); log.debug("number of colors: " + numColors); } // generate iso lines int numIso; if (numColors < 5) { numIso = 5; } else if (numColors < 10) { numIso = 2; } else { numIso = 0; } Palette isoPalette; if (numIso == 0) { // same palette isoPalette = basePalette; /* intRaster = intRaster; */ } else { isoPalette = paletteManager.getLevel(numIso); intRaster = raster.toIndexed(isoPalette); } IsoPolygonSeriesProducer ipsp = new IsoPolygonSeriesProducer( 0, 0, distance, maxDepth); numRegions = new Vectorizer(false, intRaster, rasterSize.width) .process(ipsp); IsoAttributeGenerator iag = new IsoAttributeGenerator(isoPalette); Collection<PolygonSeries> ps = ipsp.getSeries(iag); ipsp.clear(); if (debug) { log.debug("num of iso regions: " + numRegions); log.debug("num of iso series: " + ps.size()); } pds.addAllSeries(ps); columns.setInterpolation(interpolation); columns.setPolygonDataset(pds); return columns; } protected AttributedXYColumns preProcess(Collection results) { AttributedXYColumns attColumns = new AttributedXYColumns(); Map<IJKey, XYColumn> map = new HashMap<IJKey, XYColumn>(1013); Iterator iter = results.iterator(); int sIdx = -1; int iIdx = -1; int jIdx = -1; int kIdx = -1; int vIdx = -1; int zIdx = -1; while (iter.hasNext()) { Result result = (Result) iter.next(); if (sIdx == -1) { ResultDescriptor rd = result.getResultDescriptor(); int columnCount = rd.getColumnCount(); sIdx = rd.getColumnIndex("SHAPE"); iIdx = rd.getColumnIndex("IPOSITION"); jIdx = rd.getColumnIndex("JPOSITION"); kIdx = rd.getColumnIndex("KPOSITION"); vIdx = rd.getColumnIndex("YORDINATE"); zIdx = rd.getColumnIndex("Z"); for (int i = 0; i < columnCount; i++) { String colName = rd.getColumnName(i); if (!StringUtils.contains(ATTRIBUTE_LIST, colName)) { attColumns.setAttribute( colName, result.getObject(colName)); } } } double v = result.getDouble(vIdx); double z = result.getDouble(zIdx); int i = result.getInteger(iIdx); int j = result.getInteger(jIdx); int k = result.getInteger(kIdx); IJKey key = new IJKey(i, j); XYColumn col = (XYColumn)map.get(key); if (col == null) { Coordinate coord = WKTUtils.toCoordinate(result.getString(sIdx)); if (coord == null) coord = new Coordinate(); col = new XYColumn(coord.x, coord.y, i, j); map.put(key, col); } col.add(new HeightValue(z, v, k)); } ArrayList<XYColumn> cols = new ArrayList<XYColumn>(map.values()); attColumns.setXYColumns(cols); return attColumns; } @Override protected Chart getChart( ChartLabels chartLables, ChartTheme theme, Collection parameters, Collection measurements, Collection dates, Object result, Locale locale, String uuid, boolean linesVisible, boolean shapesVisible, CallContext callContext ) { Chart chart = null; if (CACHE_CHART) { log.info("Try to get verticalcrosssection chart from cache."); chart = (Chart) getChartFromCache(uuid, callContext); } if (chart != null) return chart; log.info("Chart not in cache yet."); AttributedXYColumns columns = (AttributedXYColumns)result; Integer parameterId = (Integer)columns.getAttribute("GROUP1"); // XXX: hardcoded if (parameterId == null) { log.error("missing parameter id"); return null; } Map<Integer, PaletteManager> paletteManagers = getPalettes(callContext); PaletteManager paletteManager = paletteManagers.get(parameterId); if (paletteManager == null) { log.error("no palette found for parameter id " + parameterId); return null; } chart = new VerticalCrossSectionChart( columns, paletteManager.getBase(), locale); chart.generateChart(); if (CACHE_CHART) { log.info("Put chart into cache."); purifyChart(chart, uuid); } return chart; } /** * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#createChart(java.io.OutputStream, * java.util.Collection, java.util.Collection, java.lang.String, * de.intevation.gnv.chart.ChartStyle, * de.intevation.gnv.chart.ChartLabels) */ /* @Override protected void createChart(OutputStream outputStream, Collection<KeyValueDescibeData> parameters, Collection<KeyValueDescibeData> measurements, Collection<KeyValueDescibeData> dates, ChartStyle chartStyle, ChartLabels chartLables, String uuid) throws IOException, TechnicalChartException { new VerticalCrossSectionChartFactory().createProfileChart(chartLables, chartStyle, parameters, measurements, dates, outputStream, this .getChartResult(uuid)); } */ /** * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator() */ @Override protected Statistics getStatisticsGenerator() { return new VerticalCrossSectionStatistics(); } /** * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#createCSV(java.io.OutputStream, java.util.Collection) */ @Override protected void createCSV( OutputStream outputStream, Collection<Result> chartResult ) throws UnsupportedEncodingException, IOException, StateException { /* // TODO: Implement a substitution which makes sense. if (chartResult != null) { try { CSVWriter writer = new CSVWriter(new OutputStreamWriter( outputStream, "ISO-8859-1"), ','); // USE THIS ENCODING BECAUSE OF // PROBLEMS WITH EXCEL AND UTF-8 Iterator<Result> it = chartResult.iterator(); WKTReader wktReader = new WKTReader(); while (it.hasNext()) { Result result = it.next(); int i = 0; String[] entries = new String[9]; Point p = (Point)wktReader.read(result.getString("SHAPE")); entries[i++] = ""+p.getX(); entries[i++] = ""+p.getY(); entries[i++] = result.getString("Z"); entries[i++] = result.getString("YORDINATE"); entries[i++] = result.getString("GROUP1"); entries[i++] = result.getString("GROUP2"); entries[i++] = result.getString("IPOSITION"); entries[i++] = result.getString("JPOSITION"); entries[i++] = result.getString("KPOSITION"); writer.writeNext(entries); } writer.close(); } catch (ParseException e) { log.error(e,e); throw new StateException( "Exception occured while parsing an Point from WKT."); } } else { log.error("No Data given for generating an CSV-File."); throw new StateException( "No Data given for generating an CSV-File."); } */ } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: