/*
* 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.ShapefileRecordPolyline;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.ArrayList;
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.awt.geoshape.ShapeAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.awt.geoshape.ShapeTloLinesOpen;
import org.geoforge.java.awt.geom.GfrUtilRangeLatLon;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.worldwind.formats.shapefile.OurShapefileLoaderLinesOpen;
/**
*
* @author bill
*/
public class GfrPnlContentsOkImportShapeTloLinesOpen extends GfrPnlContentsOkImportShapeTloAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrPnlContentsOkImportShapeTloLinesOpen.class.getName());
static
{
GfrPnlContentsOkImportShapeTloLinesOpen._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
public GfrPnlContentsOkImportShapeTloLinesOpen(
DocumentListener dlrParentDialog,
String[] strsExistingLabels)
{
super(dlrParentDialog, strsExistingLabels);
super._pnlSettings = new GfrPnlSettingsImportFileShapeTlo(
dlrParentDialog,
(ActionListener) this);
}
@Override
public void doJob() throws Exception
{
// IN PROGRESS
String strKeyLabel = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).getValueKeyLabel();
boolean blnIsGeometryLonLat = ((GfrPnlSettingsImportFileShapeAbs) super._pnlSettings).isValueGeometryLonLat();
String strKeyDescription = ((GfrPnlSettingsImportFileShapeTlo) super._pnlSettings).getValueKeyDescription();
String strKeyUrl = ((GfrPnlSettingsImportFileShapeTlo) super._pnlSettings).getValueKeyUrl();
for (int i=0; i<this._altRec.size(); i++)
{
ShapefileRecord sfrCur = this._altRec.get(i);
// ----
String strValueLabelCandidate = (String) sfrCur.getAttributes().getValue(strKeyLabel);
if (strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1)
{
String str = "strValueLabelCandidate==null || strValueLabelCandidate.trim().length()<1, record #" + i+1;
GfrPnlContentsOkImportShapeTloLinesOpen._LOGGER_.severe(str);
throw new Exception(str);
}
String strValueLabelUnique = super._getUniqueLabel(strValueLabelCandidate);
if (strValueLabelUnique == null)
{
String str = "by-passing duplicated label name: " + strValueLabelCandidate;
GfrPnlContentsOkImportShapeTloLinesOpen._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);
// ----
ArrayList<ArrayList<Point2D.Double>> alt = new ArrayList<ArrayList<Point2D.Double>>();
ShapefileRecordPolyline pol = (ShapefileRecordPolyline) sfrCur;
int intNbParts = pol.getNumberOfParts();
if (intNbParts < 1)
{
System.out.println("intNbParts < 1, i=" + i + ", ignoring");
continue;
}
for (int j=0; j<intNbParts; j++)
{
ArrayList<Point2D.Double> altPartCur = new ArrayList<Point2D.Double>();
Iterable<double[]> itrDoublesCur = pol.getPoints(j);
for (double[] dblsPoint : itrDoublesCur)
{
//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();
GfrPnlContentsOkImportShapeTloLinesOpen._LOGGER_.warning(strError);
throw new Exception(strError);
}
altPartCur.add(p2dLonLat);
}
alt.add(altPartCur);
}
ShapeAbs swlCur = new ShapeTloLinesOpen(strValueLabelUnique, strValueDescription, strValueUrl, alt);
super._altValue.add(swlCur);
}
}
@Override
protected boolean _check(File fle) throws Exception
{
super._check(fle);
Object objSource = (Object) fle;
super._altRec = OurShapefileLoaderLinesOpen.s_getRecordsPolylinesMultiple(objSource);
if (super._altRec==null || super._altRec.size()<1)
{
String strLog = "super._altRec==null || super._altRec.size()<1, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloLinesOpen._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();
if (brd == null)
{
String str = "brd == null, fle.getAbsolutePath()=" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloLinesOpen._LOGGER_.warning(str);
throw new Exception(str);
}
Set<Map.Entry<String, Object>> setAttributes = brd.getEntries();
if (setAttributes==null || setAttributes.isEmpty()) // TODO: let user enter an unused predix for labels
{
String strWarning = "No valid attributes associated with records of file";
strWarning += "\n" + fle.getAbsolutePath();
GfrPnlContentsOkImportShapeTloLinesOpen._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;
}
}