C# 的话建议使用ArcEngine进行开发,由于各版本不兼容,改为采用基于Java 的GeoTool进行读取

pom依赖如下

19.1

osgeo

Open Source Geospatial Foundation Repository

http://download.osgeo.org/webdav/geotools/

geosolutions

geosolutions repository

http://maven.geo-solutions.it/

true

true

org.slf4j

jcl-over-slf4j

1.7.5

org.slf4j

slf4j-api

1.7.5

org.slf4j

slf4j-log4j12

1.7.5

nl.pdok

geoserver-manager

1.7.0-pdok2

junit

junit

4.11

test

org.geotools

gt-shapefile

${geotools.version}

org.geotools

gt-swing

${geotools.version}

org.geotools

gt-jdbc

${geotools.version}

org.geotools.jdbc

gt-jdbc-postgis

${geotools.version}

org.geotools

gt-epsg-hsql

${geotools.version}

package readshape;

import org.geotools.data.FileDataStore;

import org.geotools.data.FileDataStoreFinder;

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.opengis.feature.simple.SimpleFeature;

import org.opengis.filter.Filter;

import com.vividsolutions.jts.geom.Coordinate;

import com.vividsolutions.jts.geom.Geometry;

import com.vividsolutions.jts.geom.MultiPolygon;

import com.vividsolutions.jts.geom.Polygon;

import java.io.File;

import java.io.IOException;

import java.nio.charset.Charset;

public class Read {

public static void main(String[] args){

String path1 = "E:\\Test\\TestOpenLayers\\data\\wafangdian_0\\wafangdianshi_0.shp" ;

//读取shp

SimpleFeatureCollection colls1 = readShp(path1);

//拿到所有features

SimpleFeatureIterator iters = colls1.features();

//遍历打印

while(iters.hasNext()){

SimpleFeature sf = iters.next();

Object ss= sf.getDefaultGeometry();

System.out.println(ss);

System.err.println(sf.getDefaultGeometryProperty().getValue());

Geometry shape = (Geometry)ss;

System.err.println(shape.toText());

//打印出来的都是相同的内容 MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))

// 可以直接拼接sql写入到postgresql

/*INSERT INTO table( geom )

VALUES

(

st_geomfromtext(‘MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))‘, 4326)

)

*/

// class com.vividsolutions.jts.geom.MultiPolygon

if(ss instanceof Polygon){

Polygon polygon = (Polygon)ss;

Coordinate[] coordinates= polygon.getCoordinates();

}

else if(ss instanceof MultiPolygon){

MultiPolygon multiPolygon = (MultiPolygon)ss;

String mult=multiPolygon.toString();

Coordinate[] coordinates= multiPolygon.getCoordinates();

System.err.println(coordinates.length);

}

System.err.println(ss.getClass().toString());

System.out.println(sf.getID() + " , " + sf.getAttributes());

}

}

public static SimpleFeatureCollection readShp(String path ){

return readShp(path, null);

}

public static SimpleFeatureCollection readShp(String path , Filter filter){

SimpleFeatureSource featureSource = readStoreByShp(path);

if(featureSource == null) return null;

try {

return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures() ;

} catch (IOException e) {

e.printStackTrace();

}

return null ;

}

public static SimpleFeatureSource readStoreByShp(String path ){

File file = new File(path);

FileDataStore store;

SimpleFeatureSource featureSource = null;

try {

store = FileDataStoreFinder.getDataStore(file);

((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));

featureSource = store.getFeatureSource();

} catch (IOException e) {

e.printStackTrace();

}

return featureSource ;

}

}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐