【问题标题】:Converting Shape File to RDF document, in Java在 Java 中将形状文件转换为 RDF 文档
【发布时间】:2021-09-27 01:56:26
【问题描述】:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.management.AttributeChangeNotification;

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.RDFReaderI;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.system.StreamRDFWriter;
import org.apache.jena.vocabulary.VCARD;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Query;
import org.geotools.data.ServiceInfo;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.ComplexAttribute;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.FeatureType;
import org.opengis.filter.Filter;

public class ShpToRdf {

    public static void main(String[] args) throws IOException {
        
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> values = new ArrayList<String>();

        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
              
        }

        FileDataStore myData = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource source = myData.getFeatureSource();
        SimpleFeatureType schema = source.getSchema();

        Query query = new Query(schema.getTypeName());
        query.setMaxFeatures(100);

       
        Model model = ModelFactory.createDefaultModel();
        String shpURI = "http://www.shp.fake/";

        Resource shapeFile = model.createResource(shpURI);
         
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(query);
        try (FeatureIterator<SimpleFeature> features = collection.features()) {
            while (features.hasNext()) {
                
                SimpleFeature feature = features.next();
                model.setNsPrefix("shp", shpURI);
            
                for (org.opengis.feature.Property attribute : feature.getProperties()) {
                  names.add(attribute.getName().toString());
                  values.add(attribute.getValue().toString());
                   
                }   
             }  

         }
         
         ArrayList<Integer> ids = new ArrayList<Integer>();
         for(int i=0; i<names.size();i++) {
             if (names.get(i).equals("Id")) {
                ids.add(i); 
             }
         }  

         Property features = model.createProperty(shpURI,"features");
         for(int i = 0; i<ids.size();i++) {
             Property id = model.createProperty(shpURI,names.get(ids.get(i)));
                        
             shapeFile = model.createResource(shpURI)
                        .addProperty(features, model.createResource()
                                         .addProperty(id,model.createResource()
                        .addProperty(id, values.get(ids.get(i)))
                        .addProperty(features, "feature1")
                        .addProperty(features, "feature2")
                        .addProperty(features, "feature3")));

          }

          RDFDataMgr.write(System.out, model, Lang.RDFXML);

      } 
                
            
       
}

我正在尝试创建一个将 Shape File(shp) 转换为 RDF 的应用程序。 问题是我可以从 shp 获得两个 ArrayList。一个具有值的名称(id、名称、几何等),另一个具有值。

要创建 RDF,我必须将每个 Id 与匹配值进行匹配(例如,Id =1 的名称 = 道路 1,几何形状 = 线等)

你能帮我解决这个问题吗?

谢谢!

【问题讨论】:

    标签: java rdf shapefile converters


    【解决方案1】:

    我认为您应该能够通过调整以下逻辑来做到这一点

    for (org.opengis.feature.Property attribute : feature.getProperties()) {
       names.add(attribute.getName().toString());
       values.add(attribute.getValue().toString());
    }
    

    您可以将它们放在成对的列表中,而不是将它们放在两个列表中。这样,当您遍历列表时,您就知道了主客体之间的映射关系。

    它应该看起来类似于

    List<Pair<String, Integer>> contentList = new ArrayList<Pair<String, String>>();
    
    for (org.opengis.feature.Property attribute : feature.getProperties()) {
       Pair<String, Integer> subjectObjectPairs = new Pair<String, String>(attribute.getName().toString(), attribute.getValue().toString());
       contentList.add(subjectObjectPairs);
    }
    

    我不确定 ids ArrayList 的用途,但您可以将该逻辑移到上面的 for 循环中,以确保您获取标识符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多