/* * Created on Aug 10, 2004 * * */ package anl.repast.gis.data; import java.awt.Color; import java.awt.Rectangle; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Properties; import java.util.regex.Pattern; import javax.swing.JFrame; import javax.swing.WindowConstants; import org.geotools.data.FeatureReader; import org.geotools.data.FeatureResults; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.feature.AttributeType; import org.geotools.feature.Feature; import org.geotools.feature.FeatureType; import org.geotools.filter.Filter; import org.geotools.geometry.Geometry; /* import org.geotools.data.FeatureReader; import org.geotools.data.FeatureResults; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.feature.AttributeType; import org.geotools.feature.Feature; import org.geotools.feature.FeatureType; import org.geotools.filter.Filter; import org.geotools.filter.FilterFactory; import org.geotools.geometry.Geometry; import org.geotools.gui.swing.StyledMapPane; import org.geotools.map.DefaultMapContext; import org.geotools.map.DefaultMapLayer; import org.geotools.map.MapContext; import org.geotools.pt.MismatchedDimensionException; import org.geotools.renderer.j2d.RenderedMapScale; import org.geotools.styling.AnchorPoint; import org.geotools.styling.Font; import org.geotools.styling.Graphic; import org.geotools.styling.Mark; import org.geotools.styling.PointPlacement; import org.geotools.styling.PointSymbolizer; import org.geotools.styling.PolygonSymbolizer; import org.geotools.styling.Style; import org.geotools.styling.StyleBuilder; import org.geotools.styling.StyleFactoryImpl; import org.geotools.styling.Symbolizer; import org.geotools.styling.TextSymbolizer; */ import com.bbn.openmap.InformationDelegator; import com.bbn.openmap.LatLonPoint; import com.bbn.openmap.LayerHandler; import com.bbn.openmap.MapBean; import com.bbn.openmap.MapHandler; import com.bbn.openmap.MouseDelegator; import com.bbn.openmap.dataAccess.shape.DbfTableModel; import com.bbn.openmap.dataAccess.shape.EsriGraphic; import com.bbn.openmap.dataAccess.shape.EsriGraphicList; import com.bbn.openmap.dataAccess.shape.EsriShapeExport; import com.bbn.openmap.dataAccess.shape.input.DbfInputStream; import com.bbn.openmap.dataAccess.shape.output.DbfOutputStream; import com.bbn.openmap.event.DistanceMouseMode; import com.bbn.openmap.event.NavMouseMode2; import com.bbn.openmap.event.NullMouseMode; import com.bbn.openmap.event.SelectMouseMode; import com.bbn.openmap.gui.*;//BasicMapPanel; import com.bbn.openmap.layer.*;//GraticuleLayer; import com.bbn.openmap.layer.shape.ShapeFile; import com.bbn.openmap.layer.shape.ShapeLayer; import com.bbn.openmap.omGraphics.DrawingAttributes; import com.bbn.openmap.plugin.esri.EsriLayer; import com.bbn.openmap.proj.Orthographic; import com.bbn.openmap.proj.Proj; import com.bbn.openmap.proj.Projection; import com.bbn.openmap.proj.ProjectionStack; import com.bbn.openmap.util.DataBounds; import com.vividsolutions.jts.geom.Envelope; /** * @author Robert Najlis * * */ public class OpenmapData { OpenMapFrame frame; BasicMapPanel mapPanel; MapBean mapBean; MapHandler mapHandler; ShapeLayer shapeLayer; private static OpenmapData instance = new OpenmapData(); public OpenmapData() {} public static OpenmapData getInstance() { return instance; } public void readNeighborhoodInfo(String neighborhoodFile, ArrayList neighborAcceptors) { try { Pattern p = Pattern.compile(" "); String[] data;// = new String(); int lineCount =0; String s = new String(); // ArrayList neighbors = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader(neighborhoodFile)); int agentNum = 0; in.readLine(); // deal with the header line -- just read it and do nothing while ((s = in.readLine()) != null) { // if (lineCount == 0) { // lineCount++; // continue; // } data = p.split(s); agentNum = ( Integer.parseInt(data[0]) - 1) ; // agentNum--; // System.out.println("read NBHood file: agent num : " + agentNum); if (Integer.parseInt(data[1]) > 0) { // there are neighbors // reaad the next line, parse it, and that to agent neighbor list s = in.readLine(); data = p.split(s); // neighbors = new ArrayList(); int [] neighbors = new int[data.length]; for (int i=0; i 0) s = in.readLine(); // read line but do nothing int [] noNeighbors = new int[0]; //agentHandler.addNeighborList(agentNum, noNeighbors); ((NeighborAcceptor)neighborAcceptors.get(agentNum)).setNeighbors(noNeighbors); } } // get the distance to market from raster file } catch (IOException e) { e.printStackTrace(); } } public ArrayList createAgents(Class clazz, String datasource) { System.out.println("create agents"); ArrayList agentList = new ArrayList(); try { String datasourceNoSHP = datasource.substring(0, datasource.length() - 3); // System.out.println("datasourceNoSHP " + datasourceNoSHP); String dbfString = datasourceNoSHP + "dbf"; String shxString = datasourceNoSHP + ("shx"); URL shpURL = new File(datasource).toURL(); URL dbfURL = new File(dbfString).toURL(); URL shxURL = new File(shxString).toURL(); EsriLayer esriLayer = new EsriLayer("createAgents", dbfURL, shpURL, shxURL, DrawingAttributes.DEFAULT); EsriGraphicList graphicList = esriLayer.getEsriGraphicList(); DbfTableModel dbfModel = esriLayer.getModel(); // System.out.println("esri graphic list size --- " + graphicList.size()); // DbfTableModel dbfModel = new DbfTableModel(new DbfInputStream(new FileInputStream(dbfString))); // ShapeFile shapeFile = new ShapeFile(datasource); // get feature info - features, types ) Method[] methods = clazz.getMethods(); int numFeatures = graphicList.size(); for (int i=0; i 0) { clazz = agents.iterator().next().getClass(); } // get feature info - features, types String datasourceNoSHP = origDatasource.substring(0, origDatasource.length() - 3); // System.out.println("datasourceNoSHP " + datasourceNoSHP); String dbfString = datasourceNoSHP + "dbf"; String shxString = datasourceNoSHP + ("shx"); URL shpURL = new File(origDatasource).toURL(); URL dbfURL = new File(dbfString).toURL(); URL shxURL = new File(shxString).toURL(); EsriLayer esriLayer = new EsriLayer("createAgents", dbfURL, shpURL, shxURL, DrawingAttributes.DEFAULT); EsriGraphicList graphicList = esriLayer.getEsriGraphicList(); DbfTableModel dbfModel = esriLayer.getModel(); dbfModel.setWritable(true); //FeatureReader reader = fsShape.reader(); // go through each agent String newDatasourceNoSHPNoDot = newDatasource.substring(0, newDatasource.length() - 4); DbfOutputStream dbfOut = new DbfOutputStream(new FileOutputStream(newDatasourceNoSHPNoDot + ".dbf")); dbfOut.writeModel(dbfModel); dbfOut.writeRecords(dbfModel); dbfOut.close(); /* int agentNum = -1; for (Iterator iter = agents.iterator(); iter.hasNext();) { //Feature feature = reader.next(); //featureSet.add(feature); Object agent = iter.next(); agentNum++; // go through each column in dbf int numColumns = dbfModel.getColumnCount(); for (int i = 0; i < numColumns; i++) { //AttributeType at = ft.getAttributeType(i); String columnName = dbfModel.getColumnName(i); // for each attribute type -- see if there is a get method that corresponds Method method = this.getGetMethodForName(clazz, columnName); // if there is invoke it for each object in the list if (method != null) { // invoke with data for each // System.out.println("write agents -- method : " + method.toString()); dbfModel.setValueAt(method.invoke(agent, null), agentNum, i); //System.out.println("write agents -- get value from dbf model : " + dbfModel.getValueAt(agentNum, i)); // feature.setAttribute(i, method.invoke(agent, null)); } } } */ /* dbfModel.setWritable(true); // dbfModel. String newDatasourceNoSHPNoDot = newDatasource.substring(0, newDatasource.length() - 4); EsriShapeExport export = new EsriShapeExport(graphicList, dbfModel, newDatasourceNoSHPNoDot); export.setMasterDBF(dbfModel); export.setWriteDBF(false); DbfOutputStream dbfOut = new DbfOutputStream(new FileOutputStream(newDatasourceNoSHPNoDot + ".dbf")); // export.setWritable(true); export.export(); dbfOut.writeModel(dbfModel);*/ }catch(Exception e) { e.printStackTrace(); } } public void updateDisplay() { System.out.println("update display start ..."); try { // mapHandler; shapeLayer.prepare(); //String refresh = System.getProperty("refresh.path"); //System.out.println("refresh = " + refresh); // Process p = Runtime.getRuntime().exec("c:\\agent_analyst\\Refresh\\Refresh.exe"); //p.waitFor(); // } catch (InterruptedException e) { // e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } System.out.println("update display finish"); } public Envelope getEnvelope(String datasource) { try { URL file = new File(datasource).toURL(); ShapefileDataStore store = new ShapefileDataStore(file); String name = store.getTypeNames()[0]; // there is just one - one type per shapefile - type of data in shapefile FeatureSource source = store.getFeatureSource(name); Envelope envelope = source.getBounds(); //return envelope.toString(); //return ((Envelope) source.getBounds()).toRectangle2D(); return envelope; } catch (Exception e) { e.printStackTrace(); } return null; } public double[] getCenter(Envelope envelope) { double[] centerXY = new double[2]; centerXY[0] = ((envelope.getMinX() + envelope.getMaxX())) / 2; centerXY[1] = ((envelope.getMinY() + envelope.getMaxY())) / 2; return centerXY; } public Method getSetMethodForAttributeType(Class clazz, AttributeType at) { Method method = null; Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equalsIgnoreCase("set" + at.getName())) { // System.out.println("GEt Set Method : " + methods[i].getName()); return methods[i]; } } return method; } public Method getSetMethodForName(Class clazz, String columnName) { Method method = null; Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equalsIgnoreCase("set" + columnName)) { // System.out.println("GEt Set Method : " + methods[i].getName()); return methods[i]; } } return method; } public Method getGetMethodForName(Class clazz, String name) { Method method = null; //System.out.println("GET ------ try to get get method"); try { method = clazz.getMethod("get" + name, null); } catch (NoSuchMethodException ex) { // try again but with standard java caml type name String capName = Character.toUpperCase(name.charAt(0)) + name.substring(1); try { method = clazz.getMethod("get" + capName, null); } catch (NoSuchMethodException e) { //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return method; } public Method getGetMethodForAttributeType(Class clazz, AttributeType at) { Method method = null; //System.out.println("GET ------ try to get get method"); String propName = at.getName(); try { method = clazz.getMethod("get" + propName, null); } catch (NoSuchMethodException ex) { // try again but with standard java caml type name String capName = Character.toUpperCase(propName.charAt(0)) + propName.substring(1); try { method = clazz.getMethod("get" + capName, null); } catch (NoSuchMethodException e) { //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return method; } public int getAttributePosition(String name, FeatureType ft) { AttributeType[] attributeTypes = ft.getAttributeTypes(); // go through the attributeTypes andd find which has the name that matches for (int i = 0; i < attributeTypes.length; i++) { if (attributeTypes[i].getName().equalsIgnoreCase(name)) { return i; } } return -1; } }