First you create a JMapFrame and a MapContent to put in it
JMapFrame frame = new JMapFrame();
private MapContent content = new MapContent();
public MapViewer(String[] args) {
frame.setSize(300, 300);
frame.enableToolBar(true);
frame.enableStatusBar(true);
frame.setMapContent(content);
}
Then for each Shapefile (or other datasource) you create a layer and add it
to the MapContent
HashMap<String, Object> params = new HashMap<>();
File f = new File(name);
params.put("url", URLs.fileToUrl(f));
DataStore ds = null;
try {
ds = DataStoreFinder.getDataStore(params);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ds == null) {
System.err.println("couldn't find a store for " + name);
return;
}
String type;
try {
type = ds.getTypeNames()[0];
FeatureLayer layer = new FeatureLayer(ds.getFeatureSource(type),
style);
System.out.println("adding layer " + name);
content.addLayer(layer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've attached the full example to this message.
On 11 April 2018 at 02:42, NBA浪仔 <799052...@qq.com> wrote:
> Thanks,I realize showing one map in a JPanel according to the URL(
> https://github.com/sf2gis/SwingMap), but I don't konw how to realize
> adding two map contents in a mapPane,could you give me a demo about it?
>
>
> ------------------ 原始邮件 ------------------
> *发件人:* "Ian Turton"<ijtur...@gmail.com>;
> *发送时间:* 2018年4月11日(星期三) 凌晨1:26
> *收件人:* "NBA浪仔"<799052...@qq.com>;"geotools-users"<geotools-
> gt2-us...@lists.sourceforge.net>;
> *主题:* Re: [Geotools-devel] An issue about showing more than one
> shpfilesusing Swing component
>
> Instead of creating a new mapPane for each layer you need to create just a
> new layer and add that to the existing map content object.
>
> Ian
>
> On Tue, 10 Apr 2018, 02:11 NBA浪仔, <799052...@qq.com> wrote:
>
>> Thanks a lot for your replay . Actually, I have realized the goal that
>> one panel displays one shp map ,
>> But I want to display two maps in one JPanel like this ,any suggestions?
>> Otherwise,I want to realize zooming in and out in this JPanel,how should
>> I do that?
>>
>>
>>
>> ------------------ 原始邮件 ------------------
>> *发件人:* "Ian Turton"<ijtur...@gmail.com>;
>> *发送时间:* 2018年4月10日(星期二) 凌晨0:34
>> *收件人:* "NBA浪仔"<799052...@qq.com>;
>> *抄送:* "geotools-devel"<geotools-de...@lists.sourceforge.net>;
>> *主题:* Re: [Geotools-devel] An issue about showing more than one shp
>> filesusing Swing component
>>
>> You need to create a second layer and add that to the map in exactly the
>> same way as the first.
>>
>> Ian
>>
>> On 9 April 2018 at 11:32, NBA浪仔 <799052...@qq.com> wrote:
>>
>>> Dear Sir/Madam,
>>> With your help earlier,I can display one shp format map file in
>>> a JPanel component,like this :
>>>
>>>
>>> Now I want to display two shp format map files in the Swing component
>>> --JPanel,how should I do that?
>>>
>>> Yours sincerely,
>>> Ouyang
>>>
>>>
>>>
>>>
>>> ------------------------------------------------------------
>>> ------------------
>>> Check out the vibrant tech community on one of the world's most
>>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>>> _______________________________________________
>>> GeoTools-Devel mailing list
>>> geotools-de...@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/geotools-devel
>>>
>>>
>>
>>
>> --
>> Ian Turton
>>
>
--
Ian Turton
package com.ianturton.geotools;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import javax.xml.parsers.ParserConfigurationException;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.styling.StyledLayerDescriptor;
import org.geotools.swing.JMapFrame;
import org.geotools.util.URLs;
import org.xml.sax.SAXException;
public class MapViewer {
JMapFrame frame = new JMapFrame();
private MapContent content = new MapContent();
public MapViewer(String[] args) {
frame.setSize(300, 300);
frame.enableToolBar(true);
frame.enableStatusBar(true);
frame.setMapContent(content);
for (String name : args) {
add(name);
}
}
public void add(String name) {
try {
Style style = getStyle(name);
add(name,style);
} catch (IOException | SAXException | ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(String name, Style style) {
HashMap<String, Object> params = new HashMap<>();
File f = new File(name);
params.put("url", URLs.fileToUrl(f));
DataStore ds = null;
try {
ds = DataStoreFinder.getDataStore(params);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ds == null) {
System.err.println("couldn't find a store for " + name);
return;
}
String type;
try {
type = ds.getTypeNames()[0];
FeatureLayer layer = new FeatureLayer(ds.getFeatureSource(type), style);
System.out.println("adding layer " + name);
content.addLayer(layer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param name
* @return
* @throws FileNotFoundException
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
private Style getStyle(String name)
throws FileNotFoundException, IOException, SAXException, ParserConfigurationException {
Style style = null;
String sName = name.substring(0, name.lastIndexOf('.')) + ".sld";
File s = new File(sName);
if (s.exists()) {
// create the parser with the sld configuration
org.geotools.xml.Configuration configuration = new org.geotools.sld.SLDConfiguration();
org.geotools.xml.Parser parser = new org.geotools.xml.Parser(configuration);
// the xml instance document above
InputStream xml = new FileInputStream(s);
// parse
StyledLayerDescriptor sld = (StyledLayerDescriptor) parser.parse(xml);
style = SLD.defaultStyle(sld);
}
if (style == null) {
style = SLD.createPolygonStyle(Color.BLACK, Color.gray, 1);
}
return style;
}
public void view() {
frame.setVisible(true);
}
public static void main(String[] args) {
MapViewer viewer = new MapViewer(args);
viewer.view();
}
}
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users