Package org.geoforge.guillc.panel

Source Code of org.geoforge.guillc.panel.GfrPnlContentsOkImportShapeTloLineOpen

/*
*  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.ShapeTloLineOpen;
import org.geoforge.java.awt.geom.GfrUtilRangeLatLon;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.worldwind.formats.shapefile.OurShapefileLoaderLineOpen;

/**
*
* @author bill
*/
public class GfrPnlContentsOkImportShapeTloLineOpen extends GfrPnlContentsOkImportShapeTloAbs
{
    // ----
    // begin: instantiate logger for this class
    final private static Logger _LOGGER_ = Logger.getLogger(GfrPnlContentsOkImportShapeTloLineOpen.class.getName());

    static
    {
        GfrPnlContentsOkImportShapeTloLineOpen._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
    }

    // end: instantiate logger for this class
    // ----
   
    public GfrPnlContentsOkImportShapeTloLineOpen(
            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;
                 GfrPnlContentsOkImportShapeTloLineOpen._LOGGER_.severe(str);
                 throw new Exception(str);
             }
            
             String strValueLabelUnique = super._getUniqueLabel(strValueLabelCandidate);
            
             if (strValueLabelUnique == null)
             {
                // !!!!!!!!!
                 String str = "by-passing duplicated label name: " + strValueLabelCandidate;
                 GfrPnlContentsOkImportShapeTloLineOpen._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);
    
             // ----

             ShapefileRecordPolyline pol = (ShapefileRecordPolyline) sfrCur;
            
             int intNbParts = pol.getNumberOfParts();
            
            
             if (intNbParts != 1)
             {
                String strLog = "intNbParts != 1, intNbParts=" + intNbParts + ", record #" + (i+1);
                GfrPnlContentsOkImportShapeTloLineOpen._LOGGER_.warning(strLog);
               
                String strMessage = "Record # " + (i+1);
                strMessage += " does not contain only one polyline, found: " + intNbParts;
                strMessage += "\n\nAborting";
 
                 throw new Exception(strMessage);
             }
            
             // ----
            
            
             ArrayList<Point2D.Double> alt = new ArrayList<Point2D.Double>();
            Iterable<double[]> itrDoublesCur = pol.getPoints(0);

            for (double[] dbls : itrDoublesCur)
            {
               //MEMO : working with lon lat
               Point2D.Double p2dCurLonLat = null;
           
               if (blnIsGeometryLonLat)
                  p2dCurLonLat = new Point2D.Double(dbls[0],dbls[1]);
               else
                  p2dCurLonLat = new Point2D.Double(dbls[1],dbls[0]);
              
               if (! GfrUtilRangeLatLon.s_isOk(p2dCurLonLat))
               {
                  String strError = "Wrong Latitude-Longitude value at record #" + (i+1);
                  strError += "\n . latitude: " + p2dCurLonLat.y;
                  strError += "\n . longitude: " + p2dCurLonLat.x;
                  strError += "\n\n" + "Valid values range:";
                  strError += "\n" + "Latitude: " + GfrUtilRangeLatLon.s_getMessageRangeLat();
                  strError += "\n" + "Longitude: " + GfrUtilRangeLatLon.s_getMessageRangeLon();
                  GfrPnlContentsOkImportShapeTloLineOpen._LOGGER_.warning(strError);
                  throw new Exception(strError);
               }
              
               alt.add(p2dCurLonLat);
            }

             ShapeAbs swlCur = new ShapeTloLineOpen(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 = OurShapefileLoaderLineOpen.s_getRecordsLinestring(objSource);
       
        if (super._altRec==null || super._altRec.size()<1)
        {
           String strLog = "super._altRec==null || super._altRec.size()<1, fle.getAbsolutePath()=" + fle.getAbsolutePath();
           GfrPnlContentsOkImportShapeTloLineOpen._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();
            GfrPnlContentsOkImportShapeTloLineOpen._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();
            GfrPnlContentsOkImportShapeTloLineOpen._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;
    }
}
TOP

Related Classes of org.geoforge.guillc.panel.GfrPnlContentsOkImportShapeTloLineOpen

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.