Does your shapefile have a .prj file? It's worth testing with say the
states.shp file from GeoServer or the Natural Earth datasets.

Ian

On Wed, 22 Apr 2020 at 16:05, Jonas Schrottenbaum via GeoTools-GT2-Users <
geotools-gt2-users@lists.sourceforge.net> wrote:

> Hello,
>
> i managed to get an e4 application with geotools 20.5 and also the swt
> module on 20.5 to run. But i still get exceptions when adding shapefiles or
> wms-layers to the map.
> The next step will be an attempt to upgrade the swt module version by
> version. Before that i would like to be able to display a shapefile layer
> and a wms layer on the same mappane, without problems, as a proof of
> concept.
>
> When adding a shapefile layer, i get the exception enclosed in
> exception1.txt.
>
> When adding a wms layer i get the exception enclosed in exception2.txt.
>
> Part code:
> @PostConstruct
> public void postConstruct(Composite parent, IEclipseContext context) {
>     handleImages();
>     MapContent content = new MapContent();
>     SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL | SWT.NULL);
>     Composite mainComposite = sashForm;
>     MapLayerComposite mapLayerTable = new MapLayerComposite(mainComposite,
> SWT.BORDER);
>     mapPane = new SwtMapPane(mainComposite, SWT.BORDER |
> SWT.NO_BACKGROUND);
>     mapPane.setMapContent(content);
>     mapLayerTable.setMapPane(mapPane);
>     sashForm.setWeights(new int[] { 1, 3 });
>
> mapPane.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
>     StreamingRenderer renderer = new StreamingRenderer();
>     mapPane.setRenderer(renderer);
>     context.set(SwtMapPane.class, mapPane);
> }
>
> Import shapefile code:
> @Execute
> public void execute(SwtMapPane mapPane) {
>     Display display = Display.getCurrent();
>     Shell shell = new Shell(display);
>     File file = JFileDataStoreChooser.showOpenFile(new String[] { "*.shp"
> }, shell);
>     try {
>         if (file != null && file.exists()) {
>            MapContent mapContent = mapPane.getMapContent();
>            FileDataStore store = FileDataStoreFinder.getDataStore(file);
>            SimpleFeatureSource featureSource = store.getFeatureSource();
>            Style style = Utils.createStyle(file, featureSource);
>            Layer layer = new FeatureLayer(featureSource, style);
>            //Add viewport line here
>            mapContent.addLayer(layer);
>            mapPane.redraw();
>         }
>     ...
> }
>
> Import WMS code:
> @Execute
> public void execute(SwtMapPane mapPane) {
>     WebMapServer wms = null;
>     try {
>         wms = new WebMapServer(new URL("
> http://ows.mundialis.de/services/service?";));
>     } catch ...
>     }
>     WMSCapabilities capabilities = wms.getCapabilities();
>     MapContent mapContent = mapPane.getMapContent();
>     WMSLayer layer = new WMSLayer(wms, capabilities.getLayerList().get(1));
>     //Add viewport line here
>     mapContent.addLayer(displayLayer);
>     mapPane.redraw();
> }
>
> I noticed that the SwtMapPane::getDisplayArea method (which is called
> here:  at org.geotools.swt.SwtMapPane.setCrs(SwtMapPane.java:428)) returns
> a ReferencedEnvelope where the crs is null, because the viewport of the
> mapcontent is still null.
> So i thought i need to initialize it somewhere somehow, but this is not
> shown in any tutorial i think. So maybe i'm just missing a simple line in
> the part code?
>
> I found out that adding the line
> mapContent.getViewport().setBounds(layer.getBounds()); in the shapefile/wms
> code as indicated by the comments is a quick fix for the exception, but not
> a solution because it just works for one layer.
>
> When only using shapefiles, as soon as a second one is added (from the
> same example dataset so a assume same crs) the bounds of the first one are
> overwritten by the second one and if they happen to be samller, the borders
> of the first shapefile are not displayed correctly anymore.
> And if i try to combine both, there are obviously major problems, because
> of, i assume, different crs and so on.
> When trying to display a wms layer on top of a shapefile layer, the wms
> layer is stretched and as soon as i try to change anything, like visibility
> i get an exception (enclosed in exception3.txt) and the layers won't render
> again.
> Same when trying to display a shapefile layer on top of a wmslayer, i also
> get an exception (enclosed in exception4.txt). But those are expected, as
> my "fix" only takes one layer into account.
>
> So the final question is: What am i missing to avoid those "sourceCRS
> can't be null" exceptions and what do i have to do to correctly overlay
> multiple different layers like shapefile and WMS?
> And should i follow the developers guide when trying to update the swt
> module and talk to the dev mailing list and get the contributers license
> and so on?
>
> Best regards
> Jones
>
>
> _______________________________________________
> GeoTools-GT2-Users mailing list
> GeoTools-GT2-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>


-- 
Ian Turton
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to