// ********************************************************************** // // // // BBN Technologies, a Verizon Company // 10 Moulton Street // Cambridge, MA 02138 // (617) 873-8000 // // Copyright (C) BBNT Solutions LLC. All rights reserved. // // // ********************************************************************** // // $Source$ // $RCSfile$ // $Revision$ // $Date$ // $Author$ // // ********************************************************************** package com.bbn.openmap.dataAccess.shape; import com.bbn.openmap.omGraphics.OMGraphic; import com.bbn.openmap.omGraphics.OMGraphicList; import com.bbn.openmap.omGraphics.OMPoly; import com.bbn.openmap.plugin.esri.EsriPlugIn; import com.bbn.openmap.proj.Length; import com.bbn.openmap.proj.Projection; import com.bbn.openmap.proj.ProjMath; import com.bbn.openmap.util.Debug; import java.awt.Component; import java.net.URL; import java.util.Iterator; import java.util.Properties; import javax.swing.*; public class Thinner { protected float threshold = .05f; protected DbfTableModel dbf = null; public Thinner() { EsriGraphicList list = loadShapeFile(); if (list != null) { if (Debug.debugging("thinner.describe")) { Debug.output(list.getDescription()); } list = thinData(list); writeShapeFile(list); } else { Debug.output("Thinner: Shape files couldn't be found."); } } public EsriGraphicList loadShapeFile() { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Choose a SHP file"); int returnVal = chooser.showOpenDialog((Component)null); if (returnVal==JFileChooser.APPROVE_OPTION) { String newFilename = chooser.getSelectedFile().getAbsolutePath(); EsriPlugIn epi = new EsriPlugIn(); Properties props = new Properties(); props.put(EsriPlugIn.PARAM_SHP, newFilename); epi.setProperties(props); dbf = epi.getModel(); return epi.getEsriGraphicList(); } return null; } public float getThreshold() { return threshold; } public EsriGraphicList thinData(EsriGraphicList list) { float threshold = getThreshold(); for (Iterator it = list.iterator(); it.hasNext(); ) { EsriGraphic eg = (EsriGraphic) it.next(); if (eg instanceof EsriGraphicList) { thinData((EsriGraphicList)eg); } else if (eg instanceof OMPoly) { // EsriPolygon and EsriPolyline are OMPolys OMPoly omp = (OMPoly)eg; float[] coords = omp.getLatLonArray(); // These are in radians, make decimal degrees ProjMath.arrayRadToDeg(coords); float[] newCoords = new float[coords.length]; int numCoords = coords.length; int numNewCoords = 0; float lastLat = -100f; float lastLon = -190f; float curLat, curLon; for (int i = 0; i < numCoords - 1; i+=2) { curLat = coords[i]; curLon = coords[i+1]; if (Math.abs(curLat - lastLat) > threshold && Math.abs(curLon - lastLon) > threshold) { newCoords[numNewCoords++] = curLat; newCoords[numNewCoords++] = curLon; lastLat = curLat; lastLon = curLon; } } coords = new float[numNewCoords]; System.arraycopy(newCoords, 0, coords, 0, numNewCoords); omp.setLocation(coords, OMGraphic.DECIMAL_DEGREES); } // else we don't care about points } return list; } public void writeShapeFile(EsriGraphicList list) { EsriShapeExport ese = new EsriShapeExport(list, dbf, (String)null); ese.export(); } public static void main(String[] argv) { Debug.init(); Thinner thinner = new Thinner(); System.exit(0); } }