| |
/*
* Copyright (c) 2006 ESRI
*
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* See use restrictions at /arcgis/java/samples/userestrictions.
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IDynamicMap;
import com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.system.Cleaner;
import com.esri.arcgis.system._WKSPoint;
public class DynamicTrackingCommand extends BaseCommand {
HookHelper hookHelper = null;
Timer timer = null;
ActionListener task = null;
IPoint point = null;
IActiveView activeView = null;
Vector points = new Vector();
boolean isRunning = false;
boolean once = true;
MyDynamicLayer layer = null;
int index = 0;
public DynamicTrackingCommand() {
super();
caption ="Add Dynamic Layer";
enabled = true;
}
public void onClick() {
if(once){
try{
IDynamicMap dynamicMap = (IDynamicMap)hookHelper.getFocusMap();
if (!dynamicMap.isDynamicMapEnabled()){
dynamicMap.setDynamicMapEnabled(true);
}
GenerateNavigationData();
layer = new MyDynamicLayer();
layer.setSpatialReferenceByRef(activeView.getFocusMap().getSpatialReference());
hookHelper.getFocusMap().addLayer(layer);
once = false;
}catch(Exception e){
e.printStackTrace();
}
}
if(!isRunning){
timer.start();
System.out.println("Started!");
isRunning = true;
}else{
timer.stop();
isRunning = false;
}
}
public void onCreate(Object arg0) {
try{
hookHelper = new HookHelper();
hookHelper.setHookByRef( arg0 );
activeView = hookHelper.getActiveView();
task = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
updateLayer();
}
};
timer = new Timer(100, task);
}catch(Exception e){
}
}
public IFeatureClass openFeatureClass(){
IFeatureClass fClass = null;
try{
String arcgishome = System.getenv("ARCGISHOME");
String path = arcgishome+File.separator+"java"+File.separator+"samples"+File.separator+"data"+File.separator+"usa";
ShapefileWorkspaceFactory fact = new ShapefileWorkspaceFactory();
IFeatureWorkspace workspace = (IFeatureWorkspace)fact.openFromFile(path,0);
fClass = workspace.openFeatureClass("ushigh.shp");
FeatureLayer layer = new FeatureLayer();
layer.setFeatureClassByRef(fClass);
layer.setName(fClass.getAliasName());
hookHelper.getFocusMap().addLayer(layer);
}catch(Exception e){
e.printStackTrace();
}
return fClass;
}
private void GenerateNavigationData() throws Exception
{
IEnvelope envelope = hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().getVisibleBounds();
double densificationFactor = envelope.getWidth() / 5000.0;
//open a polyline featurelayer that would serve the real-time feed GPS simulator
IFeatureClass featureclass = openFeatureClass();
if(featureclass == null){
JOptionPane.showMessageDialog(null,"ERROR");
}
//iterate through the features of the featurelayer
IFeatureCursor featureCursor = featureclass.search(null, true);
IFeature feature = null;
IPolyline polyLine = null;
IPoint pt = null;
while ((feature = featureCursor.nextFeature()) != null)
{
polyLine = (IPolyline)feature.getShapeCopy();
polyLine.smooth(0);
polyLine.densify(densificationFactor, 0);
IPointCollection pointCollection = (IPointCollection)polyLine;
int count = pointCollection.getPointCount();
for (int i = 0; i < count; i++)
{
pt = pointCollection.getPoint(i);
_WKSPoint p = new _WKSPoint();
p.x = pt.getX();
p.y = pt.getY();
points.add(p);
}
if (points.size() > 10000)
break;
}
Cleaner.release(featureCursor);
}
public void updateLayer(){
try{
if (index == (points.size() - 1))
{
System.out.println("stopping...");
timer.stop();
return;
}
_WKSPoint currentPoint = (_WKSPoint)points.elementAt(index);
_WKSPoint nextPoint = (_WKSPoint)points.elementAt(index+1);
double azimuth = (180.0 / Math.PI) * Math.atan2(nextPoint.x - currentPoint.x, nextPoint.y - currentPoint.y);
//create the navigation data structure
NavigationData navigationData = new NavigationData(currentPoint.x, currentPoint.y, azimuth);
//update the map extent and rotation
//***********************************
CenterMap(navigationData);
//m_invokeHelper.InvokeMethod(navigationData);
//add the navigation data to the loction layer in order to draw it
layer.addRow(navigationData);
//m_objectLocation.LayrIsDirty = true;
index++;
}catch(Exception e){
e.printStackTrace();
}
}
public void CenterMap(NavigationData navigationData) throws Exception
{
//get the current map visible extent
IEnvelope envelope = activeView.getScreenDisplay().getDisplayTransformation().getVisibleBounds();
if (null == point)
{
point = new Point();
}
//set the new map center coordinate
point.putCoords(navigationData.getX(), navigationData.getY());
//center the map around the new coordinate
envelope.centerAt(point);
activeView.getScreenDisplay().getDisplayTransformation().setVisibleBounds(envelope);
//rotate the map to the new rotation angle
activeView.getScreenDisplay().getDisplayTransformation().setRotation(navigationData.getAzimuth());
}
}
|