Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for srs transformation during DB export #31

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ public int getMaximumNumberOfItemsForInOperator() {
@Override
public String getFeatureHierarchyQuery() {
try {
return featureHierarchyQuery.get();
return PlainText.of(featureHierarchyQuery.get(),
"F.ENVELOPE",
"G.GEOMETRY",
"A.MULTI_POINT").toString();
} catch (Exception e) {
throw new IllegalStateException("Failed to create feature hierarchy query.", e);
}
}

@Override
public String getFeatureHierarchyQuery(int targetSRID) {
try {
return PlainText.of(featureHierarchyQuery.get(),
"st_transform(F.ENVELOPE, " + targetSRID + ")",
"st_transform(G.GEOMETRY, " + targetSRID + ")",
"st_transform(A.MULTI_POINT, " + targetSRID + ")").toString();
} catch (Exception e) {
throw new IllegalStateException("Failed to create feature hierarchy query.", e);
}
Expand Down Expand Up @@ -155,7 +170,7 @@ private String readFeatureHierarchyQuery() throws IOException {
}
}

public String readRecursiveImplicitGeometryQuery() throws IOException {
private String readRecursiveImplicitGeometryQuery() throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(
SchemaAdapter.class.getResourceAsStream("/org/citydb/database/postgres/query_recursive_implicit_geometry.sql"))))) {
return reader.lines()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ SELECT DISTINCT ON (H.ID)
F.OBJECTID,
F.IDENTIFIER,
F.IDENTIFIER_CODESPACE,
F.ENVELOPE,
{} AS ENVELOPE,
F.LAST_MODIFICATION_DATE,
F.UPDATING_PERSON,
F.REASON_FOR_UPDATE,
Expand All @@ -95,7 +95,7 @@ SELECT DISTINCT ON (H.ID)
F.TERMINATION_DATE,
F.VALID_FROM,
F.VALID_TO,
G.GEOMETRY,
{} AS GEOMETRY,
G.GEOMETRY_PROPERTIES,
G.FEATURE_ID AS GEOMETRY_FEATURE_ID,
A.OBJECTID AS ADDRESS_OBJECT_ID,
Expand All @@ -109,7 +109,7 @@ SELECT DISTINCT ON (H.ID)
A.STATE,
A.COUNTRY,
A.FREE_TEXT,
A.MULTI_POINT,
{} AS MULTI_POINT,
A.CONTENT,
A.CONTENT_MIME_TYPE
FROM FEATURE_HIERARCHY H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected SchemaAdapter(DatabaseAdapter adapter) {

public abstract String getFeatureHierarchyQuery();

public abstract String getFeatureHierarchyQuery(int targetSRID);

public abstract SqlObject getRecursiveImplicitGeometryQuery(Select featureQuery);

public abstract String getCreateIndex(Index index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package org.citydb.operation.exporter;

import org.citydb.database.adapter.DatabaseAdapter;
import org.citydb.database.geometry.SrsParseException;
import org.citydb.database.metadata.SpatialReference;
import org.citydb.database.schema.SchemaMapping;
import org.citydb.model.address.Address;
import org.citydb.model.appearance.SurfaceData;
Expand All @@ -34,6 +36,7 @@
import org.citydb.operation.exporter.util.Postprocessor;
import org.citydb.operation.exporter.util.SurfaceDataMapper;
import org.citydb.operation.exporter.util.TableHelper;
import org.citydb.sqlbuilder.util.PlainText;

import java.sql.Connection;
import java.sql.SQLException;
Expand All @@ -49,24 +52,22 @@ public class ExportHelper {
private final Postprocessor postprocessor;
private final SchemaMapping schemaMapping;
private final TableHelper tableHelper;
private final int srid;
private final String srsIdentifier;
private final SpatialReference spatialReference;
private final Set<String> featureIdCache = new HashSet<>();
private final Set<String> surfaceDataIdCache = new HashSet<>();
private final Set<String> implicitGeometryIdCache = new HashSet<>();
private final Set<String> addressIdCache = new HashSet<>();
private final Set<String> externalFileIdCache = new HashSet<>();

ExportHelper(DatabaseAdapter adapter, ExportOptions options) throws SQLException {
ExportHelper(DatabaseAdapter adapter, ExportOptions options) throws SQLException, SrsParseException {
this.adapter = adapter;
this.options = options;

connection = adapter.getPool().getConnection();
postprocessor = new Postprocessor();
schemaMapping = adapter.getSchemaAdapter().getSchemaMapping();
tableHelper = new TableHelper(this);
srid = adapter.getDatabaseMetadata().getSpatialReference().getSRID();
srsIdentifier = adapter.getDatabaseMetadata().getSpatialReference().getIdentifier();
spatialReference = adapter.getGeometryAdapter().getSpatialReference(options.getTargetSrs().orElse(null));
}

public DatabaseAdapter getAdapter() {
Expand Down Expand Up @@ -94,11 +95,11 @@ public TableHelper getTableHelper() {
}

public int getSRID() {
return srid;
return spatialReference.getSRID();
}

public String getSrsIdentifier() {
return srsIdentifier;
return spatialReference.getIdentifier();
}

public String createId() {
Expand Down Expand Up @@ -171,6 +172,14 @@ public String getInOperator(String column, Set<Long> values) {
return builder.toString();
}

public String getTransformOperator(String column) {
return adapter.getDatabaseMetadata().getSpatialReference().getSRID() == getSRID() ?
column :
adapter.getGeometryAdapter().getSpatialOperationHelper()
.transform(PlainText.of(column), getSRID())
.toString();
}

Feature exportFeature(long id, long sequenceId) throws ExportException {
try {
Feature feature = tableHelper.getOrCreateExporter(FeatureExporter.class).doExport(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

import com.alibaba.fastjson2.annotation.JSONField;
import org.citydb.config.SerializableConfig;
import org.citydb.config.common.SrsReference;
import org.citydb.core.concurrent.LazyCheckedInitializer;
import org.citydb.core.file.OutputFile;
import org.citydb.core.file.output.RegularOutputFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

@SerializableConfig(name = "exportOptions")
public class ExportOptions {
Expand All @@ -41,6 +43,7 @@ public class ExportOptions {
private OutputFile outputFile;
private int numberOfThreads;
private int numberOfTextureBuckets;
private SrsReference targetSrs;

public OutputFile getOutputFile() {
if (outputFile == null) {
Expand Down Expand Up @@ -81,4 +84,13 @@ public ExportOptions setNumberOfTextureBuckets(int numberOfTextureBuckets) {
this.numberOfTextureBuckets = numberOfTextureBuckets;
return this;
}

public Optional<SrsReference> getTargetSrs() {
return Optional.ofNullable(targetSrs);
}

public ExportOptions setTargetSrs(SrsReference targetSrs) {
this.targetSrs = targetSrs;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public AddressExporter(ExportHelper helper) throws SQLException {
super(helper);
stmt = helper.getConnection().prepareStatement("select id, objectid, identifier as address_identifier, " +
"identifier_codespace as address_identifier_codespace, street, house_number, po_box, " +
"zip_code, city, state, country, free_text, multi_point, content, content_mime_type " +
"zip_code, city, state, country, free_text, " + helper.getTransformOperator("multi_point") +
", content, content_mime_type " +
"from " + tableHelper.getPrefixedTableName(Table.ADDRESS) +
" where id = ?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private String getBaseQuery() {
"sd.x3d_shininess, sd.x3d_transparency, sd.x3d_ambient_intensity, sd.x3d_specular_color, " +
"sd.x3d_diffuse_color, sd.x3d_emissive_color, sd.x3d_is_smooth, sd.tex_image_id, " +
"sd.tex_texture_type, sd.tex_wrap_mode, sd.tex_border_color, sd.gt_orientation, " +
"sd.gt_reference_point, ti.image_uri, ti.mime_type, ti.mime_type_codespace, " +
helper.getTransformOperator("sd.gt_reference_point") + ", ti.image_uri, ti.mime_type, ti.mime_type_codespace, " +
"sdm.geometry_data_id, sdm.material_mapping, sdm.texture_mapping, sdm.world_to_texture_mapping, " +
"sdm.georeferenced_texture_mapping " +
"from " + tableHelper.getPrefixedTableName(Table.APPEARANCE) + " a " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public class FeatureExporter extends DatabaseExporter {

public FeatureExporter(ExportHelper helper) throws SQLException {
super(helper);
stmt = helper.getConnection().prepareStatement(adapter.getSchemaAdapter().getFeatureHierarchyQuery());
stmt = helper.getConnection().prepareStatement(getQuery());
}

private String getQuery() {
return adapter.getDatabaseMetadata().getSpatialReference().getSRID() == helper.getSRID() ?
adapter.getSchemaAdapter().getFeatureHierarchyQuery() :
adapter.getSchemaAdapter().getFeatureHierarchyQuery(helper.getSRID());
}

public Feature doExport(long id) throws ExportException, SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class GeometryExporter extends DatabaseExporter {

public GeometryExporter(ExportHelper helper) throws SQLException {
super(helper);
stmt = helper.getConnection().prepareStatement("select geometry, implicit_geometry, " +
"geometry_properties, feature_id as geometry_feature_id " +
stmt = helper.getConnection().prepareStatement("select " + helper.getTransformOperator("geometry") +
", implicit_geometry, geometry_properties, feature_id as geometry_feature_id " +
"from " + tableHelper.getPrefixedTableName(Table.GEOMETRY_DATA) +
" where id = ?");
}
Expand Down
Loading