将文件或geojson成形为数据库

时间:2019-02-06 02:20:23

标签: java node.js spring geotools

我正在尝试读取较大的形状文件,并尝试使用geotools查找经纬度是否在坐标中

是否可以将形状文件存储到数据库中?还是会更快地存储为geojson和reirerive以检查纬度和经度是否在坐标中? Node js或java会更容易实现。

3 个答案:

答案 0 :(得分:1)

您可以使用ogr2​​ogr工具将形状文件作为空间数据存储在MySQL中。

阅读此链接:

https://www.igismap.com/insert-shapefile-in-mysql-as-spatial-data/

答案 1 :(得分:1)

如果我错了,请纠正我,但据我了解,您将拥有Java(Spring Boot)或NodeJS后端。

我没有开发NodeJS应用的经验,但是我有使用Java的经验。我的建议是,您可以通过这种方式很快实现目标:

  • 使用最新版本的Spring Boot(2.1.2.RELEASE)。
  • 使用任何支持空间功能的数据库,例如PostgreSQL / PostGIS,Microsoft SQL Server,MySQL等。我想您还将使用任何NoSQL数据库。
  • 利用Java拓扑套件(称为JTS)或Geolatte框架将Java中的域对象建模为“几何”。我从未使用过Geolatte,但我认为它比JTS更强大。但是,JTS有两个实现:生动的解决方案'和locationtech's。前者是2012年编写的旧版本,后者是更新的版本。我总是选择每个库的最新版本,但是考虑到Spring Boot中添加了JTS依赖关系,因此我不能保证是否可以选择它。

总而言之,我选择Spring Boot 2 + JTS并将每个地理空间对象映射为Geometries。您可以检查我的Gist关于创建地理对象的信息。确定要使用的SRID。在您的实体中,将地理对象映射为Geometry数据类型。检查以下示例:

package model;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.vividsolutions.jts.geom.Geometry;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Singular;

@Entity
@Table(name="stops")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Stop implements Serializable {

    @Transient private static final long serialVersionUID = -2747601079894033016L;

    @Id private String id;
    private String codParada;
    private String tipoExtraido; 
    private String uri;
    private String titulo;
    private Geometry geometria;
    private LocalDateTime ultimaActualizacion;
    @Singular @ElementCollection private List<String> mensajes;
    private String icono;
    private String enlace;
    private String descripcion;
    @Enumerated(EnumType.STRING) private ETipoParada tipo;

}

答案 2 :(得分:1)

最简单的方法是使用数据存储打开Shapefile(或GeoJSON)文件,然后将要素从该存储复制到PostGIS(或其他Database)数据存储中。只要确保在您的maven pom中包含相关模块即可。

  public static void main(String[] args) throws MalformedURLException, IOException {
    File inFile = new File("/home/ian/Data/states/states.shp");
    Map<String, Object> outParams = new HashMap<>();
    outParams.put(PostgisNGDataStoreFactory.DBTYPE.key, PostgisNGDataStoreFactory.DBTYPE.sample);
    outParams.put(PostgisNGDataStoreFactory.USER.key, "ian");
    outParams.put(PostgisNGDataStoreFactory.PASSWD.key, "ianian");
    outParams.put(PostgisNGDataStoreFactory.HOST.key, "localhost");
    outParams.put(PostgisNGDataStoreFactory.PORT.key, 5432);
    outParams.put(PostgisNGDataStoreFactory.DATABASE.key, "ian");
    outParams.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");
    // Read
    DataStore inputDataStore = DataStoreFinder.getDataStore(
            Collections.singletonMap("url", URLs.fileToUrl(inFile)));

    String inputTypeName = inputDataStore.getTypeNames()[0];
    SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

    FeatureSource<SimpleFeatureType, SimpleFeature>
            source = inputDataStore.getFeatureSource(inputTypeName);

    FeatureCollection<SimpleFeatureType, SimpleFeature>
            inputFeatureCollection = source.getFeatures();

    DataStore newDataStore = DataStoreFinder.getDataStore(outParams);


    String typeName = inputTypeName;

    newDataStore.createSchema(inputType);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

    /*
     * //Optional Filter block //filter String geometryPropertyName =
     * inputType.getGeometryDescriptor().getLocalName();
     * CoordinateReferenceSystem targetCRS =
     * inputType.getGeometryDescriptor().getCoordinateReferenceSystem();
     * 
     * double x1 = 11.5; double y1 = 49.8; double x2 = 12.0; double y2 = 50.1;
     * 
     * ReferencedEnvelope bbox = new ReferencedEnvelope(x1, y1, x2, y2,
     * targetCRS); FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
     * Filter filter = ff.bbox(ff.property(geometryPropertyName), bbox);
     */
    // write results
    featureStore.addFeatures(source.getFeatures(/*filter*/));
    //tidy up
    inputDataStore.dispose();
    newDataStore.dispose();
    newDataStore.createSchema(inputType);
    String typeName1 = newDataStore.getTypeNames()[0];

    SimpleFeatureStore featureStore1 = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName1);

    featureStore1.addFeatures(inputFeatureCollection);

    inputDataStore.dispose();
    newDataStore.dispose();
  }