Example #1
0
 def _type_def_for_column_schema(self, col, dataset=None):
     if col.data_type == "geometry":
         crs_name = col.extra_type_info.get("geometryCRS")
         crs_id = None
         if dataset is not None:
             crs_id = (crs_util.get_identifier_int_from_dataset(
                 dataset, crs_name) or 0)
         # This user-defined GeometryType adapts Kart's GPKG geometry to SQL Server's native geometry type.
         return GeometryType(crs_id)
     elif col.data_type == "boolean":
         return BooleanType
     elif col.data_type == "float" and col.extra_type_info.get(
             "size") != 64:
         return FloatType
     elif col.data_type == "date":
         return DateType
     elif col.data_type == "numeric":
         return NumericType
     elif col.data_type == "time":
         return TimeType
     elif col.data_type == "timestamp":
         return TimestampType
     elif col.data_type == "text":
         return TextType
     else:
         # Don't need to specify type information for other columns at present, since we just pass through the values.
         return None
Example #2
0
 def _cols_to_crs_ids(self):
     result = {}
     for col in self.schema.geometry_columns:
         crs_name = col.extra_type_info.get("geometryCRS", None)
         crs_id = crs_util.get_identifier_int_from_dataset(self, crs_name)
         if crs_id:
             result[col.name] = crs_id
     return result
Example #3
0
 def generate_gpkg_contents(cls, v2_obj, table_name):
     """Generate a gpkg_contents meta item from a v2 dataset."""
     result = {
         "identifier": v2_obj.get_meta_item("title") or "",
         "description": v2_obj.get_meta_item("description"),
         "table_name": table_name,
         "data_type": "features" if v2_obj.has_geometry else "attributes",
     }
     if v2_obj.has_geometry:
         result["srs_id"] = crs_util.get_identifier_int_from_dataset(v2_obj)
     return result
Example #4
0
    def _v2_geometry_type_to_sql_type(cls, col, v2_obj=None):
        extra_type_info = col.extra_type_info
        geometry_type = extra_type_info.get("geometryType")
        if geometry_type is None:
            return "GEOMETRY"

        geometry_type = geometry_type.replace(" ", "")

        crs_id = None
        crs_name = extra_type_info.get("geometryCRS")
        if crs_name is not None and v2_obj is not None:
            crs_id = crs_util.get_identifier_int_from_dataset(v2_obj, crs_name)
        if crs_id is None:
            return f"GEOMETRY({geometry_type})"

        return f"GEOMETRY({geometry_type},{crs_id})"
Example #5
0
    def _v2_geometry_type_to_sql_type(cls, column_schema, v2_obj=None):
        extra_type_info = column_schema.extra_type_info
        geometry_type = extra_type_info.get("geometryType", "geometry")
        geometry_type_parts = geometry_type.strip().split(" ")
        if len(geometry_type_parts) > 1:
            raise NotYetImplemented(
                "Three or four dimensional geometries are not supported by MySQL working copy: "
                f'("{column_schema.name}" {geometry_type.upper()})')

        mysql_type = geometry_type_parts[0]

        crs_id = None
        crs_name = extra_type_info.get("geometryCRS")
        if crs_name is not None and v2_obj is not None:
            crs_id = crs_util.get_identifier_int_from_dataset(v2_obj, crs_name)
        if crs_id is not None:
            mysql_type += f" SRID {crs_id}"

        return mysql_type
Example #6
0
    def generate_gpkg_geometry_columns(cls, v2_obj, table_name):
        """Generate a gpkg_geometry_columns meta item from a v2 dataset."""
        geom_columns = v2_obj.schema.geometry_columns
        if not geom_columns:
            return None

        geometry_type = geom_columns[0].extra_type_info.get(
            "geometryType", "GEOMETRY")
        type_name, *zm = geometry_type.split(" ", 1)
        zm = zm[0] if zm else ""
        z = 1 if "Z" in zm else 0
        m = 1 if "M" in zm else 0

        return {
            "table_name": table_name,
            "column_name": geom_columns[0].name,
            "geometry_type_name": type_name,
            "srs_id": crs_util.get_identifier_int_from_dataset(v2_obj) or 0,
            "z": z,
            "m": m,
        }
Example #7
0
    def get_sql_type_constraints(cls, col, v2_obj=None):
        if col.data_type != "geometry":
            return None

        constraints = []

        extra_type_info = col.extra_type_info
        geometry_type = extra_type_info.get("geometryType")
        if geometry_type is not None:
            geometry_type = geometry_type.split(" ")[0].upper()
            if geometry_type != "GEOMETRY":
                constraints.append(
                    cls._geometry_type_constraint(col.name, geometry_type)
                )

        if v2_obj is not None:
            crs_name = extra_type_info.get("geometryCRS")
            crs_id = crs_util.get_identifier_int_from_dataset(v2_obj, crs_name)
            if crs_id is not None:
                constraints.append(cls._geometry_crs_constraint(col.name, crs_id))

        return f"CHECK({' AND '.join(constraints)})" if constraints else None