| | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.CoordinateTransformation; |
| | | import org.gdal.osr.SpatialReference; |
| | | import org.gdal.osr.osr; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | |
| | | return; |
| | | } |
| | | |
| | | CoordinateTransformation ct = getCoordinateTransformation(layer, isTransform); |
| | | |
| | | do { |
| | | Feature f = layer.GetNextFeature(); |
| | | if (null == f) { |
| | |
| | | } |
| | | |
| | | T t = (T) clazz.newInstance(); |
| | | readFeature(t, f, map, gField); |
| | | readFeature(t, f, map, gField, ct); |
| | | list.add(t); |
| | | } while (true); |
| | | } catch (Exception ex) { |
| | |
| | | } finally { |
| | | GdbHelper.delete(layer); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取坐标转换器 |
| | | */ |
| | | private static CoordinateTransformation getCoordinateTransformation(Layer layer, boolean isTransform) { |
| | | if (!isTransform) { |
| | | return null; |
| | | } |
| | | |
| | | String epsg = layer.GetSpatialRef().GetAttrValue("AUTHORITY", 1); |
| | | if (StringHelper.isEmpty(epsg)) { |
| | | return null; |
| | | } |
| | | |
| | | int epsgId = Integer.parseInt(epsg); |
| | | if (StaticData.I4326 == epsgId || StaticData.I4490 == epsgId) { |
| | | return null; |
| | | } |
| | | |
| | | SpatialReference srTarget = new SpatialReference(); |
| | | srTarget.ImportFromEPSG(StaticData.I4490); |
| | | |
| | | layer.GetSpatialRef().SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |
| | | srTarget.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |
| | | |
| | | return CoordinateTransformation.CreateCoordinateTransformation(layer.GetSpatialRef(), srTarget); |
| | | } |
| | | |
| | | /** |
| | |
| | | getFieldMapper(clazz.getSuperclass(), layer, map); |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | log.error(ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取Feature |
| | | */ |
| | | private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) { |
| | | private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField, CoordinateTransformation ct) { |
| | | for (Integer i : map.keySet()) { |
| | | try { |
| | | Field field = map.get(i); |
| | |
| | | } |
| | | |
| | | if (null != gField) { |
| | | setGeom(t, f, gField); |
| | | setGeom(t, gField, f.GetGeometryRef(), ct); |
| | | } |
| | | } |
| | | |
| | |
| | | * wkbNone = 100, |
| | | * wkbLinearRing = 101 |
| | | */ |
| | | private static <T> void setGeom(T t, Feature f, Field gField) { |
| | | private static <T> void setGeom(T t, Field gField, Geometry geometry, CoordinateTransformation ct) { |
| | | try { |
| | | String geo = "null"; |
| | | if (null != f.GetGeometryRef()) { |
| | | String wkt = f.GetGeometryRef().ExportToWkt(); |
| | | // f.GetGeometryRef().GetGeometryType() |
| | | if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) { |
| | | wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")"; |
| | | } |
| | | if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) { |
| | | wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")"; |
| | | } |
| | | wkt = wkt.replace(" 0,", ",").replace(" 0)", ")"); |
| | | if (wkt.contains(StaticData.MULTICURVE)) { |
| | | wkt = wkt.replace("MULTICURVE (", "MULTILINESTRING (").replace("CIRCULARSTRING ", ""); |
| | | if (wkt.contains(StaticData.COMPOUNDCURVE)) { |
| | | wkt = wkt.replace("COMPOUNDCURVE (", "").replace(")))", "))"); |
| | | } |
| | | } |
| | | |
| | | geo = String.format("ST_GeomFromText('%s')", wkt); |
| | | if (null == geometry) { |
| | | gField.set(t, "null"); |
| | | return; |
| | | } |
| | | if (null != ct) { |
| | | int flag = geometry.Transform(ct); |
| | | } |
| | | |
| | | gField.set(t, geo); |
| | | String wkt = geometry.ExportToWkt(); |
| | | if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) { |
| | | wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")"; |
| | | } |
| | | if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) { |
| | | wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")"; |
| | | } |
| | | wkt = wkt.replace(" 0,", ",").replace(" 0)", ")"); |
| | | if (wkt.contains(StaticData.MULTICURVE)) { |
| | | wkt = wkt.replace("MULTICURVE (", "MULTILINESTRING (").replace("CIRCULARSTRING ", ""); |
| | | if (wkt.contains(StaticData.COMPOUNDCURVE)) { |
| | | wkt = wkt.replace("COMPOUNDCURVE (", "").replace(")))", "))"); |
| | | } |
| | | } |
| | | |
| | | gField.set(t, String.format("ST_GeomFromText('%s')", wkt)); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |