| | |
| | | me.setBatch(StringHelper.YMDHMS_FORMAT.format(new Date(me.getCreateTime().getTime()))); |
| | | me.setCreateUser(ue.getId()); |
| | | |
| | | int rows = dataLoaderService.insertFiles(me, entity.getFileEntities(), entity.getTabEntities()); |
| | | dataLoaderService.insertFiles(me, entity.getFileEntities(), entity.getTabEntities()); |
| | | |
| | | return success("成功", rows); |
| | | return success("成功", entity.getTabEntities()); |
| | | } catch (Exception ex) { |
| | | return fail(ex.getMessage(), null); |
| | | } |
| | |
| | | this.fileName = fileName; |
| | | this.type = type; |
| | | this.tab = tab; |
| | | |
| | | } |
| | | |
| | | public TabMapperEntity(String fileName, String type, String tab, String subPath) { |
| | |
| | | private String subPath; |
| | | |
| | | private String entity; |
| | | |
| | | private Integer rows; |
| | | |
| | | public String getFileName() { |
| | | return fileName; |
| | |
| | | public void setEntity(String entity) { |
| | | this.entity = entity; |
| | | } |
| | | |
| | | public Integer getRows() { |
| | | return rows; |
| | | } |
| | | |
| | | public void setRows(Integer rows) { |
| | | this.rows = rows; |
| | | } |
| | | } |
| | |
| | | |
| | | private String geom; |
| | | |
| | | private String uname; |
| | | |
| | | private String depName; |
| | | |
| | | private String verName; |
| | | |
| | | public MetaEntity() { |
| | | } |
| | | |
| | |
| | | public void setGeom(String geom) { |
| | | this.geom = geom; |
| | | } |
| | | |
| | | public String getUname() { |
| | | return uname; |
| | | } |
| | | |
| | | public void setUname(String uname) { |
| | | this.uname = uname; |
| | | } |
| | | |
| | | public String getDepName() { |
| | | return depName; |
| | | } |
| | | |
| | | public void setDepName(String depName) { |
| | | this.depName = depName; |
| | | } |
| | | |
| | | public String getVerName() { |
| | | return verName; |
| | | } |
| | | |
| | | public void setVerName(String verName) { |
| | | this.verName = verName; |
| | | } |
| | | } |
| | |
| | | |
| | | return file.substring(start + 1); |
| | | } |
| | | |
| | | /** |
| | | * 获取路径 |
| | | * |
| | | * @param file 文件 |
| | | * @return 文件路径 |
| | | */ |
| | | public static String getPath(String file) { |
| | | if (StringHelper.isEmpty(file)) { |
| | | return null; |
| | | } |
| | | |
| | | int end = file.lastIndexOf(File.separator); |
| | | |
| | | return file.substring(0, end); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private static Object getProperty(Feature feature, int index) { |
| | | public static Object getProperty(Feature feature, int index) { |
| | | int type = feature.GetFieldType(index); |
| | | |
| | | PropertyGetter propertyGetter; |
| | |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.ogr.DataSource; |
| | | import org.gdal.ogr.Layer; |
| | | import org.gdal.ogr.ogr; |
| | | import org.gdal.ogr.*; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.sql.Timestamp; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * GDB帮助类 |
| | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | Layer layer = dataSource.GetLayer(i); |
| | | |
| | | list.add(layer.GetName()); |
| | | |
| | | layer.delete(); |
| | | } |
| | | |
| | | driver.delete(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 读取数据 |
| | | */ |
| | | public static <T> List<T> readData(Class clazz, String filePath, String layerName) { |
| | | List<T> list = new ArrayList<>(); |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | if (driver == null) { |
| | | return list; |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | Layer layer = dataSource.GetLayer(i); |
| | | |
| | | if (layer.GetName().equals(layerName)) { |
| | | GdbHelper.readLayer(clazz, layer, list); |
| | | break; |
| | | } |
| | | |
| | | layer.delete(); |
| | | } |
| | | |
| | | driver.delete(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 读取图层 |
| | | */ |
| | | public static <T> void readLayer(Class clazz, Layer layer, List<T> list) { |
| | | try { |
| | | Field gField = getGeomField(clazz); |
| | | Map<Integer, Field> map = getFieldMapper(clazz, layer); |
| | | if (map.size() == 0) { |
| | | return; |
| | | } |
| | | |
| | | do { |
| | | Feature f = layer.GetNextFeature(); |
| | | if (null == f) { |
| | | break; |
| | | } |
| | | |
| | | T t = (T) clazz.newInstance(); |
| | | if (readFeature(t, f, map, gField)) { |
| | | list.add(t); |
| | | } |
| | | } while (true); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | if (null != layer) { |
| | | layer.delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取 geom 字段 |
| | | */ |
| | | private static Field getGeomField(Class clazz) { |
| | | try { |
| | | Field gField = clazz.getDeclaredField("geom"); |
| | | gField.setAccessible(true); |
| | | |
| | | return gField; |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取字段映射 |
| | | */ |
| | | private static <T> Map<Integer, Field> getFieldMapper(Class clazz, Layer layer) { |
| | | Map<Integer, Field> map = new HashMap<>(3); |
| | | |
| | | try { |
| | | FeatureDefn fd = layer.GetLayerDefn(); |
| | | for (int i = 0, count = fd.GetFieldCount(); i < count; i++) { |
| | | FieldDefn fieldDefn = fd.GetFieldDefn(i); |
| | | try { |
| | | Field field = clazz.getDeclaredField(fieldDefn.GetName().toLowerCase()); |
| | | field.setAccessible(true); |
| | | |
| | | map.put(i, field); |
| | | } catch (Exception e) { |
| | | // |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 读取Feature |
| | | */ |
| | | private static <T> boolean readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) { |
| | | try { |
| | | for (Integer i : map.keySet()) { |
| | | Field field = map.get(i); |
| | | setValue(t, f, field, i); |
| | | } |
| | | |
| | | if (null != gField) { |
| | | setGeom(t, f, gField); |
| | | } |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置值 |
| | | */ |
| | | private static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception { |
| | | switch (field.getType().toString()) { |
| | | case "class java.math.BigDecimal": |
| | | case "class java.lang.Double": |
| | | case "double": |
| | | field.set(t, f.GetFieldAsDouble(i)); |
| | | break; |
| | | case "class java.lang.Long": |
| | | case "long": |
| | | field.set(t, f.GetFieldAsInteger64(i)); |
| | | break; |
| | | case "class java.lang.Integer": |
| | | case "int": |
| | | field.set(t, f.GetFieldAsInteger(i)); |
| | | break; |
| | | case "class java.sql.Timestamp": |
| | | field.set(t, getTimestamp(f, i)); |
| | | break; |
| | | default: |
| | | field.set(t, f.GetFieldAsString(i)); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置 geom 字段值 |
| | | */ |
| | | private static <T> void setGeom(T t, Feature f, Field gField) throws Exception { |
| | | Geometry geometry = f.GetGeometryRef(); |
| | | if (null != geometry) { |
| | | gField.set(t, geometry.ExportToWkt()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取 Timestamp |
| | | */ |
| | | private static Timestamp getTimestamp(Feature f, int index) { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | | int[] pnHour = new int[1]; |
| | | int[] pnMinute = new int[1]; |
| | | float[] pfSecond = new float[1]; |
| | | int[] pnTzFlag = new int[1]; |
| | | |
| | | f.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTzFlag); |
| | | |
| | | float fSecond = pfSecond[0]; |
| | | int s = (int) fSecond; |
| | | int ns = (int) (1000000000 * fSecond - s); |
| | | |
| | | LocalDateTime localDateTime = LocalDateTime.of( |
| | | LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]), |
| | | LocalTime.of(pnHour[0], pnMinute[0], s, ns) |
| | | ); |
| | | |
| | | Timestamp timestamp = Timestamp.valueOf(localDateTime); |
| | | |
| | | return timestamp; |
| | | } |
| | | } |
| | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | Layer layer = dataSource.GetLayer(i); |
| | | |
| | | list.add(layer.GetName()); |
| | | |
| | | layer.delete(); |
| | | } |
| | | |
| | | driver.delete(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 读取数据 |
| | | */ |
| | | public static <T> List<T> readData(Class clazz, String filePath, String layerName) { |
| | | List<T> list = new ArrayList<>(); |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("PGeo"); |
| | | if (driver == null) { |
| | | return list; |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | Layer layer = dataSource.GetLayer(i); |
| | | |
| | | if (layer.GetName().equals(layerName)){ |
| | | GdbHelper.readLayer(clazz, layer, list); |
| | | break; |
| | | } |
| | | |
| | | layer.delete(); |
| | | } |
| | | |
| | | driver.delete(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取数据 |
| | | */ |
| | | public static <T> List<T> readData(Class clazz, String filePath) { |
| | | List<T> list = new ArrayList<>(); |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | | if (driver == null) { |
| | | return list; |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath); |
| | | Layer layer = dataSource.GetLayer(0); |
| | | |
| | | GdbHelper.readLayer(clazz, layer, list); |
| | | |
| | | driver.delete(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | } |
| | |
| | | package com.lf.server.service.data; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.lf.server.entity.all.BaseEntity; |
| | | import com.lf.server.entity.ctrl.TabMapperEntity; |
| | | import com.lf.server.entity.data.MetaEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.lf.server.helper.*; |
| | | import com.lf.server.mapper.all.BasicMapper; |
| | | import com.lf.server.mapper.all.GeomBaseMapper; |
| | | import com.lf.server.service.all.BaseQueryService; |
| | | import com.lf.server.service.all.BaseUploadService; |
| | |
| | | /** |
| | | * 插入文件 |
| | | */ |
| | | public int insertFiles(MetaEntity entity, List<MetaFileEntity> list, List<TabMapperEntity> tabs) { |
| | | int count = 0; |
| | | public void insertFiles(MetaEntity entity, List<MetaFileEntity> list, List<TabMapperEntity> tabs) { |
| | | try { |
| | | String temp = pathHelper.getConfig().getTempPath(); |
| | | String root = pathHelper.getUploadFullPath(); |
| | | String full = pathHelper.getUploadFullPath(); |
| | | String root = pathHelper.getConfig().getUploadPath(); |
| | | |
| | | for (MetaFileEntity mf : list) { |
| | | String filePath = getFilePath(temp, root, mf); |
| | | String filePath = getFilePath(temp, full, mf); |
| | | if (null == filePath) { |
| | | continue; |
| | | } |
| | |
| | | } |
| | | |
| | | // 入库 |
| | | if (null != me.getType() && insertDb(me, mfe, tabs) > 0) { |
| | | count++; |
| | | switch (me.getType()) { |
| | | case "xls": |
| | | insertXls(root, mfe, tabs); |
| | | break; |
| | | case "shp": |
| | | case "gdb": |
| | | case "mdb": |
| | | String zipPath = FileHelper.getPath(temp + File.separator + mf.getPath()) + "_zip"; |
| | | insertDb(me.getType(), zipPath, mfe, tabs); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 插入数据库 |
| | | */ |
| | | private Integer insertDb(MetaEntity me, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | String root = pathHelper.getConfig().getUploadPath(); |
| | | switch (me.getType()) { |
| | | case "xls": |
| | | return insertXls(root, mfe, tabs); |
| | | case "mdb": |
| | | return insertMdb(root, mfe, tabs); |
| | | case "shp": |
| | | return insertShp(root, mfe, tabs); |
| | | case "gdb": |
| | | return insertGdb(root, mfe, tabs); |
| | | default: |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 插入Excel |
| | | */ |
| | | private Integer insertXls(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | private void insertXls(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | TabMapperEntity tab = getTabEntity(mfe, tabs); |
| | | if (null == tab) { |
| | | return 0; |
| | | if (null == tab || StringHelper.isEmpty(tab.getEntity())) { |
| | | return; |
| | | } |
| | | |
| | | BaseMapper baseMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == baseMapper) { |
| | | return 0; |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == basicMapper) { |
| | | return; |
| | | } |
| | | |
| | | String className = ClassHelper.getClassName(baseMapper); |
| | | String className = ClassHelper.getClassName(basicMapper); |
| | | Class clazz = ClassHelper.getEntityClass(className); |
| | | if (null == clazz) { |
| | | return 0; |
| | | return; |
| | | } |
| | | |
| | | List<?> list = ExcelHelper.readExcel(clazz, root + File.separator + mfe.getPath()); |
| | | if (null == list || list.isEmpty()) { |
| | | return 0; |
| | | if (list.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | setCreateInfo(list, mfe); |
| | | |
| | | int rows = 0; |
| | | for (int i = 0, c = list.size(); i < c; i++) { |
| | | rows += baseMapper.insert(list.get(i)); |
| | | rows += basicMapper.insert(list.get(i)); |
| | | } |
| | | if (basicMapper instanceof GeomBaseMapper) { |
| | | updateXlsGeom((GeomBaseMapper) basicMapper, list); |
| | | } |
| | | |
| | | if (baseMapper instanceof GeomBaseMapper) { |
| | | GeomBaseMapper geomBaseMapper = (GeomBaseMapper) baseMapper; |
| | | updateGeom(geomBaseMapper, list); |
| | | tab.setRows(rows); |
| | | } |
| | | |
| | | return rows; |
| | | /** |
| | | * 获取表映射实体类 |
| | | */ |
| | | private TabMapperEntity getTabEntity(MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (tab.getFileName().equals(mfe.getName())) { |
| | | return tab; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 设置创建信息 |
| | | */ |
| | | @SuppressWarnings("AlibabaRemoveCommentedCode") |
| | | private <T> void setCreateInfo(List<T> list, MetaFileEntity mfe) { |
| | | try { |
| | | for (T t : list) { |
| | |
| | | /** |
| | | * 设置空间信息 |
| | | */ |
| | | private <T> void updateGeom(GeomBaseMapper geomBaseMapper, List<T> list) { |
| | | private <T> void updateXlsGeom(GeomBaseMapper geomBaseMapper, List<T> list) { |
| | | try { |
| | | String tabName = BaseQueryService.getTabName(geomBaseMapper); |
| | | for (T t : list) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 插入MDB |
| | | * 插入DB |
| | | */ |
| | | private Integer insertMdb(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * 插入SHP |
| | | */ |
| | | private Integer insertShp(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * 插入GDB |
| | | */ |
| | | private Integer insertGdb(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * 获取表映射实体类 |
| | | */ |
| | | private TabMapperEntity getTabEntity(MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | private void insertDb(String type, String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (tab.getFileName().equals(mfe.getName())) { |
| | | return tab; |
| | | if (!tab.getFileName().equals(mfe.getName()) || StringHelper.isEmpty(tab.getEntity())) { |
| | | continue; |
| | | } |
| | | |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == basicMapper) { |
| | | continue; |
| | | } |
| | | |
| | | String className = ClassHelper.getClassName(basicMapper); |
| | | Class clazz = ClassHelper.getEntityClass(className); |
| | | if (null == clazz) { |
| | | continue; |
| | | } |
| | | |
| | | List<?> list = null; |
| | | switch (type) { |
| | | case "shp": |
| | | list = ShpHelper.readData(clazz, root + File.separator + tab.getSubPath()); |
| | | break; |
| | | case "gdb": |
| | | list = GdbHelper.readData(clazz, root + File.separator + tab.getSubPath(), tab.getTab()); |
| | | break; |
| | | case "mdb": |
| | | list = MdbHelper.readData(clazz, root + File.separator + tab.getSubPath(), tab.getTab()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | if (null == list || list.isEmpty()) { |
| | | continue; |
| | | } |
| | | setCreateInfo(list, mfe); |
| | | |
| | | Integer rows = basicMapper.insertBatch(list); |
| | | if (basicMapper instanceof GeomBaseMapper) { |
| | | updateDbGeom((GeomBaseMapper) basicMapper, list); |
| | | } |
| | | |
| | | tab.setRows(rows); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | /** |
| | | * 设置空间信息 |
| | | */ |
| | | private <T> void updateDbGeom(GeomBaseMapper geomBaseMapper, List<T> list) { |
| | | try { |
| | | String tabName = BaseQueryService.getTabName(geomBaseMapper); |
| | | for (T t : list) { |
| | | BaseEntity baseEntity = (BaseEntity) t; |
| | | Integer gid = baseEntity.getGid(); |
| | | |
| | | Field gField = t.getClass().getDeclaredField("geom"); |
| | | gField.setAccessible(true); |
| | | Object obj = gField.get(t); |
| | | |
| | | if (null != obj) { |
| | | geomBaseMapper.updateGeom(tabName, gid, (String) obj); |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |