查询
创建shp
数据准备
let param= { code : "utf-8" ,
datalist : [
{ name : "地方" , type : 0 , heigh : 2.5 , geom : "POINT(109.588 34.645)" } ,
{ name : "煽动反" , type : 2 , heigh : 3.2 , geom : "POINT(109.588 34.645)" }
] ,
filepath : "D:gisdata/gp/aaa/sx_test.shp" ,
geomfiled : "geom" ,
keylist : [
{
filedname : "name" ,
type : "string"
} , {
filedname : "type" ,
type : "int"
} , {
filedname : "heigh" ,
type : "double"
}
] ,
type : "Point"
}
shp文件创建及添加数据
public void write2Shape ( String shpPath, String encode, String geoType, String shpKey, List < ShpFiled > attrKeys, List < Map < String , Object > > data) {
WKTReader reader = new WKTReader ( ) ;
try {
File file = new File ( shpPath) ;
Map < String , Serializable > params = new HashMap < > ( ) ;
params. put ( ShapefileDataStoreFactory . URLP . key, file. toURI ( ) . toURL ( ) ) ;
ShapefileDataStore ds = ( ShapefileDataStore ) new ShapefileDataStoreFactory ( ) . createNewDataStore ( params) ;
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder ( ) ;
tb. setCRS ( DefaultGeographicCRS . WGS84 ) ;
tb. setName ( "sx_test" ) ;
tb. add ( "the_geom" , getClass ( geoType) ) ;
for ( ShpFiled field : attrKeys) {
tb. add ( field. getFiledname ( ) . toUpperCase ( ) , getClass ( field. getType ( ) ) ) ;
}
ds. createSchema ( tb. buildFeatureType ( ) ) ;
Charset charset = Charset . forName ( encode) ;
ds. setCharset ( charset) ;
FeatureWriter < SimpleFeatureType , SimpleFeature > writer = ds. getFeatureWriter ( ds. getTypeNames ( ) [ 0 ] , Transaction . AUTO_COMMIT ) ;
for ( int i = 0 ; i < data. size ( ) ; i++ ) {
SimpleFeature feature = writer. next ( ) ;
Map < String , Object > row = data. get ( i) ;
Geometry geom = reader. read ( row. get ( shpKey) . toString ( ) ) ;
feature. setAttribute ( "the_geom" , geom) ;
for ( ShpFiled field : attrKeys) {
if ( row. get ( field. getFiledname ( ) ) != null ) {
feature. setAttribute ( field. getFiledname ( ) . toUpperCase ( ) , row. get ( field. getFiledname ( ) ) ) ;
} else {
feature. setAttribute ( field. getFiledname ( ) . toUpperCase ( ) , null ) ;
}
}
}
writer. write ( ) ;
writer. close ( ) ;
ds. dispose ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
shp新增数据
数据准备
{ filename:"D:\gisdata\gp\sx_qxzd.shp" , list : [ { 'name' : 'dfs' , 'geom' : 'POINT(108.555 34.645)' } ] }
开始新增
public static void addFeature ( String path, List < Map < String , Object > > datalist, String code) {
ShapefileDataStore dataStore = null ;
File file = new File ( path) ;
try {
dataStore = new ShapefileDataStore ( file. toURL ( ) ) ;
Charset charset = Charset . forName ( code) ;
dataStore. setCharset ( charset) ;
String typeName = dataStore. getTypeNames ( ) [ 0 ] ;
SimpleFeatureStore store = ( SimpleFeatureStore ) dataStore. getFeatureSource ( typeName) ;
SimpleFeatureType featureType = store. getSchema ( ) ;
List < String > fileds = getFileds ( featureType) ;
List < SimpleFeature > list = new ArrayList < SimpleFeature > ( ) ;
WKTReader reader = new WKTReader ( ) ;
for ( Map < String , Object > data : datalist) {
SimpleFeatureBuilder build = new SimpleFeatureBuilder ( featureType) ;
if ( data. get ( "geom" ) != null ) {
Geometry geometry = reader. read ( data. get ( "geom" ) . toString ( ) ) ;
build. add ( geometry) ;
} else {
build. add ( null ) ;
}
for ( String filed : fileds) {
Object object = data. get ( filed) ;
build. add ( object) ;
}
SimpleFeature feature = build. buildFeature ( null ) ;
list. add ( feature) ;
}
SimpleFeatureCollection collection = new ListFeatureCollection ( featureType, list) ;
Transaction transaction = new DefaultTransaction ( "Adding" ) ;
store. setTransaction ( transaction) ;
try {
store. addFeatures ( collection) ;
transaction. commit ( ) ;
System . out. println ( "============addFeature=====done=====" ) ;
} catch ( Exception eek) {
eek. printStackTrace ( ) ;
transaction. rollback ( ) ;
}
} catch ( MalformedURLException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
}
shp更新数据
数据准备
{ filename必填:"D:\\gisdata\\gp\\sx_qxzd.shp" , list : [ { 'name' : 'dfs' , 'geom' : 'POINT(108.555 34.645)' } , where : 'FID_1=425' ] }
开始更新
public static void updateFeature ( String path, List < Map < String , Object > > datalist, String code) {
ShapefileDataStore dataStore = null ;
File file = new File ( path) ;
Transaction transaction = new DefaultTransaction ( "handle" ) ;
try {
dataStore = new ShapefileDataStore ( file. toURL ( ) ) ;
Charset charset = Charset . forName ( code) ;
dataStore. setCharset ( charset) ;
String typeName = dataStore. getTypeNames ( ) [ 0 ] ;
SimpleFeatureStore store = ( SimpleFeatureStore ) dataStore. getFeatureSource ( typeName) ;
SimpleFeatureType featureType = store. getSchema ( ) ;
List < String > fileds = getFileds ( featureType) ;
store. setTransaction ( transaction) ;
WKTReader reader = new WKTReader ( ) ;
for ( Map < String , Object > data : datalist) {
Filter filter = null ;
if ( data. get ( "where" ) != null ) {
filter = ECQL . toFilter ( data. get ( "where" ) . toString ( ) ) ;
}
Object [ ] objs = new Object [ ] { } ;
String [ ] str = new String [ ] { } ;
if ( data. get ( "geom" ) != null ) {
Geometry geometry = reader. read ( data. get ( "geom" ) . toString ( ) ) ;
str = add ( str, "the_geom" ) ;
objs = add ( objs, geometry) ;
}
for ( String stri : fileds) {
if ( data. get ( stri) != null ) {
str = add ( str, stri) ;
objs = add ( objs, data. get ( stri) ) ;
}
}
store. modifyFeatures ( str, objs, filter) ;
}
transaction. commit ( ) ;
System . out. println ( "========updateFeature====end====" ) ;
} catch ( Exception eek) {
eek. printStackTrace ( ) ;
try {
transaction. rollback ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
shp移除数据
数据准备
{ filename:"D:\gisdata\gp\sx_qxzd.shp" , ids : [ 2 ,5 ,6 ] , filed : "id" }
开始移除
public static void removeFeature ( String path, List < String > ids, String filed, String code) {
ShapefileDataStore dataStore = null ;
File file = new File ( path) ;
Transaction transaction = new DefaultTransaction ( "handle" ) ;
try {
dataStore = new ShapefileDataStore ( file. toURL ( ) ) ;
Charset charset = Charset . forName ( code) ;
dataStore. setCharset ( charset) ;
String typeName = dataStore. getTypeNames ( ) [ 0 ] ;
SimpleFeatureStore store = ( SimpleFeatureStore ) dataStore. getFeatureSource ( typeName) ;
store. setTransaction ( transaction) ;
Filter filter = null ;
if ( ids. size ( ) > 0 ) {
String join = filed + " in (" + StringUtils . join ( ids, "," ) + ")" ;
System . out. println ( join) ;
filter = ECQL . toFilter ( join) ;
}
if ( filter!= null ) {
store. removeFeatures ( filter) ;
transaction. commit ( ) ;
System . out. println ( "======removeFeature== done ========" ) ;
}
} catch ( Exception eek) {
eek. printStackTrace ( ) ;
try {
transaction. rollback ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}