/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.guillc.panel;
import gov.nasa.worldwind.formats.shapefile.DBaseRecord;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecord;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecordMultiPoint;
import gov.nasa.worldwind.formats.shapefile.ShapefileRecordPoint;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.event.DocumentListener;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.java.awt.geom.GfrUtilRangeLatLon;
import org.geoforge.java.lang.system.StopWatch;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.worldwind.formats.shapefile.OurShapefileLoaderPoint;
/**
*
* @author bill
*/
abstract public class GfrPnlContentsOkImportShapeTloPoint2dAbs extends GfrPnlContentsOkImportShapeTloAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrPnlContentsOkImportShapeTloPoint2dAbs.class.getName());
static
{
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
abstract protected void _doJobPoint_(
String strKeyLabel,
String strKeyDescription,
String strKeyUrl,
boolean blnIsGeometryLonLat) throws Exception;
abstract protected void _doJobMultiPointUnique_(
String strKeyLabel,
String strKeyDescription,
String strKeyUrl,
boolean blnIsGeometryLonLat) throws Exception;
protected GfrPnlContentsOkImportShapeTloPoint2dAbs(
DocumentListener dlrParentDialog,
String[] strsExistingLabels)
{
super(dlrParentDialog, strsExistingLabels);
}
@Override
public void doJob() throws Exception
{
StopWatch.start(
GfrPnlContentsOkImportShapeTloPoint2dAbs.class.getName()
+ ".doJob(), super._altRec.size()="
+ super._altRec.size());
String strKeyLabel = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).getValueKeyLabel();
boolean blnIsGeometryLonLat = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).isValueGeometryLonLat();
String strKeyDescription = ((GfrPnlSettingsImportFileShapeTlo) super._pnlSettings).getValueKeyDescription();
String strKeyUrl = ((GfrPnlSettingsImportFileShapeTlo) super._pnlSettings).getValueKeyUrl();
// assuming homogeneous shapes in list
// assuming at least one record
ShapefileRecord sfrFirst = this._altRec.get(0);
if (sfrFirst instanceof ShapefileRecordMultiPoint)
{
_doJobMultiPointUnique_(strKeyLabel, strKeyDescription, strKeyUrl, blnIsGeometryLonLat);
}
else
{
_doJobPoint_(strKeyLabel, strKeyDescription, strKeyUrl, blnIsGeometryLonLat);
}
//System.out.println(StopWatch.getTime());
}
protected Point2D.Double _getValueLonLatPoint2doubleFromMultiPoint(
ShapefileRecord sfrCur,
boolean blnIsGeometryLonLat,
int intCount) throws Exception
{
Iterable<double[]> itrCur = ((ShapefileRecordMultiPoint) sfrCur).getPoints(0);
// TODO: check for unique value
//MEMO : working with LON LAT
Point2D.Double p2dLonLat = null;
for (double[] dblsPoint : itrCur)
{
if (blnIsGeometryLonLat)
p2dLonLat = new Point2D.Double(dblsPoint[0], dblsPoint[1]);
else
p2dLonLat = new Point2D.Double(dblsPoint[1], dblsPoint[0]);
if (!GfrUtilRangeLatLon.s_isOk(p2dLonLat))
{
String strError = "Wrong Latitude-Longitude value at record #" + (intCount + 1);
strError += "\n . latitude: " + p2dLonLat.y;
strError += "\n . longitude: " + p2dLonLat.x;
strError += "\n\n" + "Valid values range:";
strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(strError);
throw new Exception(strError);
}
}
return p2dLonLat;
}
protected Point2D.Double _getValueLonLatPoint2doubleFromPoint(
ShapefileRecord sfrCur,
boolean blnIsGeometryLonLat,
int intCount) throws Exception
{
ShapefileRecordPoint recPoint = (ShapefileRecordPoint) sfrCur;
double[] dblsPoint = recPoint.getPoint();
Point2D.Double p2dLonLat = null;
//MEMO : working with LON LAT
if (blnIsGeometryLonLat)
p2dLonLat = new Point2D.Double(dblsPoint[0], dblsPoint[1]);
else
p2dLonLat = new Point2D.Double(dblsPoint[1], dblsPoint[0]);
if (!GfrUtilRangeLatLon.s_isOk(p2dLonLat))
{
String strError = "Wrong Longitude-Latitude value at record #" + (intCount + 1);
strError += "\n . latitude: " + p2dLonLat.y;
strError += "\n . longitude: " + p2dLonLat.x;
strError += "\n\n" + "Valid values range:";
strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(strError);
throw new Exception(strError);
}
return p2dLonLat;
}
protected String _getValueLabel(ShapefileRecord sfrCur, String strKeyLabel) throws Exception
{
String strValueLabelCandidate = (String) sfrCur.getAttributes().getValue(strKeyLabel);
if (strValueLabelCandidate == null || strValueLabelCandidate.trim().length() < 1)
{
String str = "strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1";
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.severe(str);
throw new Exception(str);
}
return strValueLabelCandidate;
}
/*{
for (int i = 0; i < this._altRec.size(); i++)
{
ShapefileRecord sfrCur = this._altRec.get(i);
// ----
String strValueLabelCandidate = _getValueLabel(sfrCur, strKeyLabel);
String strValueLabelUnique = super._getUniqueLabel(strValueLabelCandidate);
if (strValueLabelUnique == null)
{
String str = "by-passing duplicated label name: " + strValueLabelCandidate;
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(str);
GfrOptionPaneAbs.s_showDialogWarning(null, str);
continue;
}
// ----
String strValueDescription = null;
if (strKeyDescription != null && strKeyDescription.trim().length() > 0)
strValueDescription = (String) sfrCur.getAttributes().getValue(strKeyDescription);
// ----
String strValueUrl = null;
if (strKeyUrl != null && strKeyUrl.trim().length() > 0)
strValueUrl = (String) sfrCur.getAttributes().getValue(strKeyUrl);
// ----
Iterable<double[]> itrCur = ((ShapefileRecordMultiPoint) sfrCur).getPoints(0);
// TODO: check for unique value
for (double[] dblsPoint : itrCur)
{
//MEMO : working with LON LAT
Point2D.Double p2dLonLat = null;
if (blnIsGeometryLonLat)
p2dLonLat = new Point2D.Double(dblsPoint[0], dblsPoint[1]);
else
p2dLonLat = new Point2D.Double(dblsPoint[1], dblsPoint[0]);
if (!GfrUtilRangeLatLon.s_isOk(p2dLonLat))
{
String strError = "Wrong Latitude-Longitude value at record #" + (i + 1);
strError += "\n . latitude: " + p2dLonLat.y;
strError += "\n . longitude: " + p2dLonLat.x;
strError += "\n\n" + "Valid values range:";
strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(strError);
throw new Exception(strError);
}
ShapeAbs swlCur = new ShapeTloPoint2d(strValueLabelUnique, strValueDescription, strValueUrl, p2dLonLat);
super._altValue.add(swlCur);
}
}
}*/
@Override
protected boolean _check(File fle) throws Exception
{
super._check(fle);
Object objSource = (Object) fle;
super._altRec = OurShapefileLoaderPoint.s_getRecords(objSource);
if (super._altRec == null || super._altRec.size() < 1)
{
String strLog = "super._altRec==null || super._altRec.size()<1, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(strLog);
String strWarning = "No valid records found in file";
strWarning += "\n" + fle.getAbsolutePath();
throw new Exception(strWarning);
}
ShapefileRecord sfr1 = super._altRec.get(0);
DBaseRecord brd = sfr1.getAttributes();
Set<Map.Entry<String, Object>> setAttributes = brd.getEntries();
if (brd == null)
{
String str = "brd == null, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(str);
throw new Exception(str);
}
if (setAttributes == null || setAttributes.isEmpty()) // TODO: let user enter an unused prefix for labels
{
String strWarning = "No valid attributes associated with records of file";
strWarning += "\n" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloPoint2dAbs._LOGGER_.warning(strWarning);
GfrOptionPaneAbs.s_showDialogError(null, strWarning);
return false;
}
super._strsAtrributeKey = new String[setAttributes.size()];
int intCount = 0;
Iterator itr = setAttributes.iterator();
while (itr.hasNext())
{
Map.Entry<String, Object> map = (Map.Entry<String, Object>) itr.next();
String strKey = map.getKey();
super._strsAtrributeKey[intCount] = strKey;
intCount++;
}
return true;
}
}