Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

gis - Geotools write an feature collection array into the same json file using FeatureJSON class

I want to implement getfeatureinfo with geotools , i already writed the code to get a given feature info in a given bbox ,using x,y and buffer parameters,my probleme is that when the clicked area contain multiple layers it will return a list of feature collection instead of one feature collection when using one layer in the map.so my probleme is with the featureJSON class it can encode one feature collection only ,so how to make featureJSON class write a multiple feature collection with different schemas or feature types ,another question is where there is any other class that can do the job rather encoding with FeatureJSON class? Here my getFeatureInfo method:

@Override
public List<SimpleFeatureCollection> getFeatureInfo(GetFeatureInfoRequest request) throws IOException {
        double x1, y1, x2, y2;
        int width, height, i, j;
        try {
            x1 = Double.parseDouble(request.getBbox()[0]);
            y1 = Double.parseDouble(request.getBbox()[1]);
            x2 = Double.parseDouble(request.getBbox()[2]);
            y2 = Double.parseDouble(request.getBbox()[3]);
            width = request.getWidth();
            height = request.getHeight();
            i = request.getX();
            j = request.getY();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }

        // Calculate the map coordinates of the selected area
        double cx1, cy1, cx2, cy2;

        cx1 = x1 * (width - i + 0.5 + request.getBuffer()) / width + x2 * (i - 0.5 - request.getBuffer()) / width;
        cx2 = x1 * (width - i + 0.5 - request.getBuffer()) / width + x2 * (i - 0.5 + request.getBuffer()) / width;
        cy1 = y1 * (j - 0.5 + request.getBuffer()) / height + y2 * (height - j + 0.5 - request.getBuffer()) / height;
        cy2 = y1 * (j - 0.5 - request.getBuffer()) / height + y2 * (height - j + 0.5 + request.getBuffer()) / height;
        ReferencedEnvelope clickArea = new ReferencedEnvelope(cx1, cx2, cy1, cy2, DefaultGeographicCRS.WGS84);

        List<Layer> mapLayers = request.getMap().getLayers();
        mapLayers.sort(new Comparator<Layer>() {
            @Override
            public int compare(Layer o1, Layer o2) {
                return (int) (o1.getOrder() - o2.getOrder());
            }
        });

        List<SimpleFeatureCollection> results = new ArrayList<>(mapLayers.size());

        Filter filter = ff.bbox(ff.property("geom"), clickArea);

        for (Layer layer : mapLayers) {
            postgisDataStore.addVirtualTable(layer.getSlug(), layer.getSqlQuery());
            SimpleFeatureSource featureSource = postgisDataStore.getFeatureSource(layer.getSlug());
            SimpleFeatureCollection fc = featureSource.getFeatures(filter);
            results.add(fc);

        }

        return results;
    }

After that i write my feature collection list with featureJSON:

GetFeatureInfoRequest featureInfoRequest = buildGetFeatureInfoRequest(request);
            List<SimpleFeatureCollection> fc = getMapService.getFeatureInfo(featureInfoRequest);
            OutputStream os = response.getOutputStream();
            FeatureJSON featureJson = new FeatureJSON();

            for (SimpleFeatureCollection collection : fc) {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                featureJson.writeFeatureCollection(collection, outputStream);
                outputStream.writeTo(os);
                outputStream.close();
            }
            os.flush();
            os.close();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...