| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.BaseGeoEntity; |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.mapper.all.GeomBaseMapper; |
| | | import com.lf.server.service.all.BaseQueryService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.SpatialReference; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.sql.Timestamp; |
| | |
| | | /** |
| | | * 获取字段映射 |
| | | */ |
| | | private static <T> void getFieldMapper(Class clazz, Layer layer, Map<Integer, Field> map) { |
| | | private static void getFieldMapper(Class clazz, Layer layer, Map<Integer, Field> map) { |
| | | try { |
| | | FeatureDefn fd = layer.GetLayerDefn(); |
| | | for (int i = 0, count = fd.GetFieldCount(); i < count; i++) { |
| | |
| | | } |
| | | } |
| | | |
| | | if (OBJECT != clazz.getSuperclass().getName()) { |
| | | if (!OBJECT.equals(clazz.getSuperclass().getName())) { |
| | | getFieldMapper(clazz.getSuperclass(), layer, map); |
| | | } |
| | | } catch (Exception ex) { |
| | |
| | | |
| | | return Timestamp.valueOf(localDateTime); |
| | | } |
| | | |
| | | /** |
| | | * 创建GDB |
| | | */ |
| | | public static void createGdb(String filePath, Map<String, List<?>> map) { |
| | | Driver driver = null; |
| | | DataSource dataSource = null; |
| | | try { |
| | | driver = ogr.GetDriverByName("FileGDB"); |
| | | if (null == driver) { |
| | | return; |
| | | } |
| | | dataSource = driver.CreateDataSource(filePath, null); |
| | | if (null == dataSource) { |
| | | return; |
| | | } |
| | | |
| | | for (String key : map.keySet()) { |
| | | Layer layer = null; |
| | | try { |
| | | GeomBaseMapper baseMapper = ClassHelper.getGeoBaseMapper(key); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | | layer = createLayer(dataSource, baseMapper); |
| | | if (null == layer) { |
| | | continue; |
| | | } |
| | | |
| | | String className = ClassHelper.getClassName(baseMapper); |
| | | Class clazz = ClassHelper.getEntityClass(className); |
| | | if (null == clazz) { |
| | | continue; |
| | | } |
| | | |
| | | List<Field> fields = new ArrayList<>(); |
| | | fields.add(getGeomField(clazz)); |
| | | getFields(clazz, fields); |
| | | addLayerField(layer, fields); |
| | | |
| | | setLayerData(layer, fields, map.get(key)); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } finally { |
| | | if (null != layer) { |
| | | layer.delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | dataSource.SyncToDisk(); |
| | | dataSource.FlushCache(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | GdbHelper.delete(dataSource, driver); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建图层 |
| | | */ |
| | | private static Layer createLayer(DataSource dataSource, GeomBaseMapper baseMapper ) { |
| | | String tab = BaseQueryService.getTabName(baseMapper); |
| | | if (StringHelper.isNull(tab)) { |
| | | return null; |
| | | } |
| | | |
| | | String geomType = baseMapper.selectGeometryType(tab); |
| | | if (StringHelper.isEmpty(geomType)) { |
| | | return null; |
| | | } |
| | | |
| | | Integer srid = baseMapper.selectSrid(tab); |
| | | SpatialReference sr = new SpatialReference(); |
| | | sr.ImportFromEPSG(null == srid ? 4326 : srid); |
| | | |
| | | return dataSource.CreateLayer(tab, sr, getGeomType(geomType), null); |
| | | } |
| | | |
| | | /** |
| | | * 获取Geom类别 |
| | | */ |
| | | private static Integer getGeomType(String geomType) { |
| | | if (StringHelper.isEmpty(geomType)) { |
| | | return ogr.wkbUnknown; |
| | | } |
| | | |
| | | switch (geomType) { |
| | | case "ST_Point": |
| | | return ogr.wkbPoint; |
| | | case "ST_LineString": |
| | | return ogr.wkbLineString; |
| | | case "ST_MultiLineString": |
| | | return ogr.wkbMultiLineString; |
| | | case "ST_Polygon": |
| | | return ogr.wkbPolygon; |
| | | case "ST_MultiPolygon": |
| | | return ogr.wkbMultiPolygon; |
| | | default: |
| | | return ogr.wkbUnknown; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取字段 |
| | | */ |
| | | private static void getFields(Class clazz, List<Field> list) { |
| | | try { |
| | | Field[] fields = clazz.getDeclaredFields(); |
| | | for (Field f : fields) { |
| | | if (StaticData.GDB_EXCLUDE_FIELDS.contains(f.getName())) { |
| | | continue; |
| | | } |
| | | |
| | | f.setAccessible(true); |
| | | list.add(f); |
| | | } |
| | | |
| | | if (!OBJECT.equals(clazz.getSuperclass().getName())) { |
| | | getFields(clazz.getSuperclass(), list); |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加图层字段 |
| | | */ |
| | | private static void addLayerField(Layer layer, List<Field> list) { |
| | | for (int i = 1, c = list.size(); i < c; i++) { |
| | | Field f = list.get(i); |
| | | |
| | | int fieldType = getFieldType(f); |
| | | FieldDefn fd = new FieldDefn(f.getName(), fieldType); |
| | | |
| | | layer.CreateField(fd, i); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取字段类型 |
| | | */ |
| | | private static Integer getFieldType(Field f) { |
| | | switch (f.getType().getName()) { |
| | | case "java.math.BigDecimal": |
| | | case "java.lang.Double": |
| | | case "double": |
| | | return ogr.OFTReal; |
| | | case "java.lang.Long": |
| | | case "long": |
| | | return ogr.OFTInteger64; |
| | | case "java.lang.Integer": |
| | | case "int": |
| | | return ogr.OFTInteger; |
| | | case "java.sql.Timestamp": |
| | | return ogr.OFTDateTime; |
| | | default: |
| | | return ogr.OFTString; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置图层数据 |
| | | */ |
| | | private static <T> void setLayerData(Layer layer, List<Field> fields, List<T> list) throws Exception { |
| | | for (T t : list) { |
| | | Feature f = new Feature(layer.GetLayerDefn()); |
| | | |
| | | BaseGeoEntity geoEntity = (BaseGeoEntity) t; |
| | | Geometry geom = Geometry.CreateFromWkt(geoEntity.getGeom()); |
| | | f.SetGeometry(geom); |
| | | |
| | | for (int i = 1, c = fields.size(); i < c; i++) { |
| | | Field field = fields.get(i); |
| | | Object val = field.get(t); |
| | | if (null == val) { |
| | | continue; |
| | | } |
| | | |
| | | switch (field.getType().getName()) { |
| | | case "java.math.BigDecimal": |
| | | case "java.lang.Double": |
| | | case "double": |
| | | double d = (double) val; |
| | | f.SetField(i, d); |
| | | break; |
| | | case "java.lang.Long": |
| | | case "long": |
| | | long l = (long) val; |
| | | f.SetField(i, l); |
| | | break; |
| | | case "java.lang.Integer": |
| | | case "int": |
| | | int n = (int) val; |
| | | f.SetField(i, n); |
| | | break; |
| | | case "java.sql.Timestamp": |
| | | Timestamp time = (Timestamp) field.get(t); |
| | | setTimestamp(f, i, time); |
| | | break; |
| | | default: |
| | | String str = (String) val; |
| | | f.SetField(i, str); |
| | | break; |
| | | } |
| | | } |
| | | layer.CreateFeature(f); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置Timestamp |
| | | */ |
| | | @SuppressWarnings("AlibabaRemoveCommentedCode") |
| | | private static void setTimestamp(Feature f, int i, Timestamp time) { |
| | | if (null == time) { |
| | | return; |
| | | } |
| | | |
| | | //Calendar now = Calendar.getInstance(); |
| | | //now.setTimeInMillis(time.getTime()); |
| | | // f.SetField(i, now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, now.get(Calendar.DATE), now.get(Calendar.HOUR), now.get(Calendar.MINUTE), now.get(Calendar.SECOND), 8); |
| | | |
| | | LocalDateTime local = time.toLocalDateTime(); |
| | | f.SetField(i, local.getYear(), local.getMonthValue(), local.getDayOfMonth(), local.getHour(), local.getMinute(), local.getSecond(), 8); |
| | | } |
| | | } |