I'm trying to build a simple gui with a JStreamNavMap and a
JStreamNavigationBar. The problem I'm having is that when the gui first
starts the map does not display. I can click the "Zoom All" button on
the nav bar, and then the map will appear correctly. I'm attaching the
code.
Any ideas why the map isn't showing up on startup?
Matt
--
Do not go where the path may lead; go instead where there is no path and leave
a trail.
-- Ralph Waldo Emerson
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gov.noaa.nws.GFEDomainMover;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.collections.map.SingletonMap;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.gui.swing.map.map2d.stream.JStreamNavMap;
import org.geotools.gui.swing.map.map2d.stream.control.JStreamNavigationBar;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.DefaultMapLayer;
import org.geotools.map.MapContext;
import org.geotools.referencing.CRS;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.SLDParser;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.geotools.styling.StyleFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
/**
*
* @author mwfoster
*/
public class MoverGUI2 extends JFrame {
private final JStreamNavMap map = new JStreamNavMap();
private final JStreamNavigationBar navBar = new JStreamNavigationBar();
private final String workPath = System.getProperty("user.dir");
public MoverGUI2() {
initComponents();
MapContext context = buildContext();
navBar.setMap(map);
map.getRenderingStrategy().setContext(context);
}
private void initComponents() {
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
//map.setSize(800, 800);
map.setPreferredSize(new Dimension(800, 800));
navBar.setFloatable(false);
JPanel panNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
panNorth.add(navBar);
add(BorderLayout.NORTH, panNorth);
add(BorderLayout.CENTER, map);
}
private DefaultMapContext buildContext() {
System.out.println("Setting MapContext...");
DefaultMapLayer shapeLayer;
Style st = readStyleInfo()[0];
FeatureTypeStyle[] ftStyles = st.getFeatureTypeStyles();
DataStore store;
File shapefile;
Style s;
CoordinateReferenceSystem crs = null;
String wkt = "PROJCS[\"Special LCC\"," +
"GEOGCS[\"WGS 84\"," +
"DATUM[\"WGS_1984\"," +
"SPHEROID[\"WGS 84\",6378137,298.257223563," +
"AUTHORITY[\"EPSG\",\"7030\"]]," +
"TOWGS84[0,0,0,0,0,0,0]," +
"AUTHORITY[\"EPSG\",\"6326\"]]," +
"PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," +
"UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," +
"AUTHORITY[\"EPSG\",\"4326\"]]," +
"PROJECTION[\"Lambert_Conformal_Conic_1SP\"]," +
"UNIT[\"m\", 1]," +
"AXIS[\"X\",EAST]," +
"AXIS[\"Y\",NORTH]," +
"PARAMETER[\"latitude_of_origin\",25.0]," +
"PARAMETER[\"central_meridian\",-95.0]," +
"PARAMETER[\"false_easting\",0.0]," +
"PARAMETER[\"false_northing\",0.0]]";
try {
//crs = CRS.decode("EPSG:4326");
crs = CRS.parseWKT(wkt);
} catch (Exception e) {
System.out.println("CRS Exception!");
e.printStackTrace();
System.exit(1);
}
DefaultMapContext mc = new DefaultMapContext(crs);
try {
// States
shapefile = new File(workPath + "/LocalStates_LCC.shp");
store = DataStoreFinder.getDataStore(new SingletonMap("url",
shapefile.toURL()));
if (null == store) {
System.out.println("store is null!");
}
FeatureSource<SimpleFeatureType, SimpleFeature> fsState =
store.getFeatureSource(store.getTypeNames()[0]);
s = getStyle(ftStyles[0]);
shapeLayer = new DefaultMapLayer(fsState, s);
mc.addLayer(shapeLayer);
// Counties
shapefile = new File(workPath + "/OK_TX_LCC.shp");
store = DataStoreFinder.getDataStore(new SingletonMap("url",
shapefile.toURL()));
FeatureSource<SimpleFeatureType, SimpleFeature> fsCounty =
store.getFeatureSource(store.getTypeNames()[0]);
s = getStyle(ftStyles[1]);
shapeLayer = new DefaultMapLayer(fsCounty, s);
mc.addLayer(shapeLayer);
// Cities
shapefile = new File(workPath + "/CitiesGT1000_LCC.shp");
store = DataStoreFinder.getDataStore(new SingletonMap("url",
shapefile.toURL()));
FeatureSource<SimpleFeatureType, SimpleFeature> fsCity =
store.getFeatureSource(store.getTypeNames()[0]);
s = getStyle(ftStyles[2]);
shapeLayer = new DefaultMapLayer(fsCity, s);
mc.addLayer(shapeLayer);
shapefile = null;
} catch (Exception e) {
System.out.println("Oh dear.");
e.printStackTrace();
System.exit(1);
}
//ReferencedEnvelope envelope = new ReferencedEnvelope(-550451, -57006,
910904, 1380511, crs);
//mc.setAreaOfInterest(envelope);
return mc;
}
private Style[] readStyleInfo() {
Style[] sa;
SLDParser sp = null;
File sld = new File(System.getProperty("user.dir") + "/SLD.xml");
StyleBuilder stb = new StyleBuilder();
StyleFactory sf = stb.getStyleFactory();
try {
sp = new SLDParser(sf, sld);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sa = sp.readXML();
return sa;
}
private Style getStyle(FeatureTypeStyle fts) {
StyleBuilder sb = new StyleBuilder();
Style s = sb.createStyle();
s.addFeatureTypeStyle(fts);
return s;
}
}
begin:vcard
fn:Matt Foster - N0EYE
n:Foster;Matt
org:;NWS Forecast Office - Norman OK
email;internet:[email protected]
title:Information Technology Officer
tel;work:(405) 325-3406
x-mozilla-html:TRUE
version:2.1
end:vcard
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users