| | |
| | | try { |
| | | // String转Geometry |
| | | geo = new WKTReader(new GeometryFactory(new PrecisionModel())).read(s); |
| | | // Geometry转WKB |
| | | byte[] geometryBytes = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN, false).write(geo); |
| | | // 设置SRID为mysql默认的 0 |
| | | byte[] wkb = new byte[geometryBytes.length + 4]; |
| | | wkb[0] = wkb[1] = wkb[2] = wkb[3] = 0; |
| | | System.arraycopy(geometryBytes, 0, wkb, 4, geometryBytes.length); |
| | | preparedStatement.setBytes(i, wkb); |
| | | } catch (ParseException e) { |
| | | |
| | | // 解析SRID(如果字符串包含SRID=4326;前缀) |
| | | int srid = 4326; // 默认使用WGS84坐标系 |
| | | String wktString = s; |
| | | if (s != null && s.toUpperCase().startsWith("SRID=")) { |
| | | int semicolonIndex = s.indexOf(';'); |
| | | if (semicolonIndex > 0) { |
| | | try { |
| | | srid = Integer.parseInt(s.substring(5, semicolonIndex)); |
| | | wktString = s.substring(semicolonIndex + 1); |
| | | } catch (NumberFormatException e) { |
| | | // 如果解析失败,使用默认SRID 4326 |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 设置Geometry的SRID |
| | | geo.setSRID(srid); |
| | | |
| | | // Geometry转WKB |
| | | byte[] geometryBytes = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN, true).write(geo); |
| | | preparedStatement.setBytes(i, geometryBytes); |
| | | } catch (ParseException e) { |
| | | throw new SQLException("Failed to parse geometry WKT: " + s, e); |
| | | } |
| | | } |
| | | |