Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileOutputState.java @ 418:6eae1efb5fc3
Horizontales Schnittprofil:
Added some code to prevent needless null inserts if interpolating over large gaps.
gnv-artifacts/trunk@466 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 21 Dec 2009 14:41:18 +0000 |
parents | 6491000407dd |
children | bed9735adf84 |
line wrap: on
line source
/** * */ package de.intevation.gnv.state.profile.horizontal; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.io.ParseException; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.Locale; import org.apache.log4j.Logger; import org.jfree.chart.ChartTheme; import de.intevation.gnv.chart.Chart; import de.intevation.gnv.chart.ChartLabels; import de.intevation.gnv.chart.HorizontalProfileChart; import de.intevation.gnv.exports.DefaultExport; import de.intevation.gnv.exports.ShapeDataCollector; import de.intevation.gnv.exports.DefaultProfile; import de.intevation.gnv.exports.Export.Profile; import de.intevation.gnv.geobackend.base.Result; 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.HorizontalProfileStatistics; import de.intevation.gnv.statistics.Statistics; /** * @author Tim Englich <tim.englich@intevation.de> * */ public class HorizontalProfileOutputState extends TimeSeriesOutputState { public static final String [] HORIZONTAL_PROFILE_COLUMNS = { "SHAPE", "YORDINATE", "GROUP1" // "GROUP2", }; public static final String [] HORIZONTAL_MESH_CSV_COLUMN_LABEL = { "Longitude", "Latitude", "Value", "ParameterID", //"MeshID" }; public static final String [] HORIZONTAL_MEASUREMENT_CSV_COLUMN_LABEL = { "Longitude", "Latitude", "Value", "ParameterID", //"SurveyID" }; /** * The UID of this class */ private static final long serialVersionUID = 4401516087492028840L; private static Logger log = Logger .getLogger(HorizontalProfileOutputState.class); public static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss"; public static final String [] CHART_TITLE_META = { "CRUISE", "DEPTH", "SHAPE" }; public static final String [] CHART_TITLE_META_RESSOURCES = { "cruiseid", "depth", "coordinate" }; public static final String [] TIMESERIES_CSV_PROFILE_NAMES = { "SHAPE", "YORDINATE", "GROUP1", "GROUP2", "GROUP3" }; public static final Profile TIMESERIES_CSV_PROFILE = new DefaultProfile( null, ',', '"', '"', "CSV", "ISO-8859-1"); /** * Constructor */ public HorizontalProfileOutputState() { super(); super.domainLable = "chart.horizontalprofile.title.xaxis"; } @Override protected Chart getChart( ChartLabels chartLables, ChartTheme theme, Collection parameters, Collection measurements, Collection dates, Collection result, Locale locale, String uuid, boolean linesVisible, boolean shapesVisible ) { Chart chart = null; if (CACHE_CHART) { log.info("Try to get horizontalprofile chart from cache."); chart = (Chart) getChartFromCache(uuid); } if (chart != null) return chart; log.info("Chart not in cache yet."); chart = new HorizontalProfileChart( chartLables, theme, parameters, measurements, dates, result, null, locale, linesVisible, shapesVisible ); chart.generateChart(); if (CACHE_CHART) { log.info("Put chart into cache."); purifyChart(chart, uuid); } return chart; } /** * @see de.intevation.gnv.state.timeseries.TimeSeriesOutputState#getStatisticsGenerator() */ @Override protected Statistics getStatisticsGenerator() { return new HorizontalProfileStatistics(); } @Override protected void createCSV(OutputStream out, Collection<Result> results) throws UnsupportedEncodingException, IOException, StateException { log.debug("Create csv export for horizontal profiles."); Iterator iter = results.iterator(); Result res = iter.hasNext() ? (Result) iter.next() : null; if (res == null) return; Profile profile = null; int dataid = res.getInteger("DATAID").intValue(); // on meshes if (dataid == 2) { profile = new DefaultProfile( HORIZONTAL_MESH_CSV_COLUMN_LABEL, ',', '"', '"', "CSV", "ISO-8859-1"); } // on measurements else { profile = new DefaultProfile( HORIZONTAL_MEASUREMENT_CSV_COLUMN_LABEL, ',', '"', '"', "CSV", "ISO-8859-1"); } DefaultExport export = new DefaultExport( new ShapeDataCollector(HORIZONTAL_PROFILE_COLUMNS)); export.create(profile, out, results); } protected String createChartTitle(Locale locale, String uuid) { String fisName = getFisName(locale); log.debug("created title for horizontal profile chart: " + fisName); return fisName; } protected String createChartSubtitle(Locale locale, String uuid) { log.debug("create chart subtitle."); String subtitle = createTimePeriod(locale, uuid); // ODV results contain meta information about cruise, station and so on Collection results = getODVResult(uuid); if (results != null) { Iterator iter = results.iterator(); Result result = iter.hasNext() ? (Result) iter.next() : null; subtitle += subtitle.length() != 0 ? "\n" : ""; subtitle += createMetaChartSubtitle(locale, result); } return subtitle; } protected String createMetaChartSubtitle(Locale locale, Result result) { log.debug("Fetch meta information and put it into subtitle."); if (result == null) return ""; StringBuilder meta = new StringBuilder(); WKTReader wktReader = new WKTReader(); for (int i = 0; i < CHART_TITLE_META.length; i++) { String qry = CHART_TITLE_META[i]; if (qry.equals("SHAPE")) { try { Point p = (Point) wktReader.read(result.getString(qry)); meta.append(getMessage(locale,"coordinate","coordinate")); meta.append(": "); log.debug( "Add " + qry + " to meta information of subtitle: " + p.toString() ); meta.append(p.getX() + ", " + p.getY()); } catch (ParseException pe) { log.warn("Error while parsing point.", pe); } } else { log.debug( "Add " + qry + " to meta information of subtitle: " + result.getString(qry) ); meta.append(getMessage( locale, CHART_TITLE_META_RESSOURCES[i], CHART_TITLE_META_RESSOURCES[i] )); meta.append(": "); meta.append(result.getString(qry)); } if (i != CHART_TITLE_META.length-1) meta.append("\n"); } log.debug("Meta title for chart: " + meta.toString()); return meta.toString(); } protected String createTimePeriod(Locale locale, String uuid) { log.debug("create time period for chart subtitle."); String subTitle = null; Date startDate = null; Date endDate = null; Collection dates = getDates(uuid); if (dates == null) { log.debug("No time period for subtitle."); return ""; } SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); KeyValueDescibeData data = null; Iterator iter = dates.iterator(); while (iter.hasNext()) { try { data = (KeyValueDescibeData)iter.next(); if (!data.isSelected()) continue; Date current = format.parse(data.getValue()); long time = current.getTime(); if (startDate == null) { startDate = current; endDate = current; } else if (time < startDate.getTime()) { startDate = current; } else if (time > endDate.getTime()) { endDate = current; } } catch (java.text.ParseException pe) { log.warn("Error while parsing date: " + data.getValue(), pe); } } if (startDate != null && endDate != null) subTitle = format.format(startDate)+" - "+format.format(endDate); subTitle = subTitle == null ? "" : subTitle; log.debug("created title for horizontal profile chart: " + subTitle); return subTitle; } }