Package: cjdb
cjdbcjdbis a Python-based importer/exporter ofCityJSONL files (CityJSON Lines)to and from a PostgreSQL database.
It requires thePostGISextension.InstallationpipinstallcjdbIt is recommended to install it in an isolated environment.UsageCheck ourdocs onlineorcjdb--helpQuickstartSample CityJSON data can be downloaded fromthe 3DBAG download service.
For example,download the tile "9-284-556", within which part of TU Delft is located.
Then, having downloaded the CityJSON file, you need a combination ofcjio(external CityJSON processing library) and cjdb to import it to a schema in a database.  Here is a step-by-step guide:Convert CityJSON to CityJSONLcjio--suppress_msg9-284-556.jsonexportjsonl9-284-556.jsonlCreate a new database called "testcjdb"If you installed PostgreSQL you should have the program 'createdb', socreatedb testcjdbAlternatively, you can use PgAdmin,see how.Import CityJSONL to the database in the schema "cjdb"cjdbimport-Hlocalhost-Upostgres-dtestcjdb-scjdb-f9-284-556.jsonlAlternatively steps 1 and 3 in a single command:cjio--suppress_msg9-284-556.jsonexportjsonlstdout|cjdbimport-Hlocalhost-Upostgres-dpostgres-scjdbThe metadata and the objects can then be found in the tables in the specified schema (cjdbin this example).The password can be specified in thePGPASSWORDenvironment variable. If not specified, the app will prompt for the password.If you want to export from the database you have two options. You can export the whole database in a CityJSONL file with:cjdbexport-Hlocalhost-Upostgres-dtestcjdb-scjdb-oresult.jsonlor export only part of it, using a select query as input. The select query should return the ids of the objects to be exported:cjdbexport-Hlocalhost-Upostgres-dtestcjdb-scjdb-oresult.jsonl-q"SELECT 'NL.IMBAG.Pand.1655100000500568' as object_id"If you want to convert from CityJSONFeatures to city json you can usecjio:cat/path/to/result.city.jsonl|cjiostdinsave/path/to/output.city.jsonUsing dockerBuild:dockerbuild-tcjdb:latest.Run:dockerrun--rm-itcjdbcjdb--helpTo import some files, the-voption is needed to mount our local file directory in the container:dockerrun-v{MYDIRECTORY}:/data--rm-it--network=hostcjdbcjdbimport-Hlocalhost-Upostgres-dpostgres-Wpostgres-f/data/5870_ext.jsonlImportant NotesData modelThecjdbimporter loads the data in accordance with aspecific data model.For example SQL queries on the tables seehereIndexesSome indexes are created when a new schema is created (refer toData Model).In addition to these indexes, the user can add more indexes on certain  CityObject attributes with the-x/--attr-indexor the-px/--partial-attr-indexflags.
We recommend these additional indexes for attributes that are frequently queried.
The second option uses a partial index with anot nullcondition on the attribute.
This saves disk space when indexing an attribute that is not present among all the imported CityObjects.
This is often the case with CityJSON, because in a single dataset there can be different object types, with different attributes.Structuring the database and its schemasIt is recommended to group together semantically coherent objects, by importing them to the same database schema.
One database can have different schemas.While the current data model supports the import of any type of CityJSON objects together (BuildingandSolitaryVegetationObject), the data becomes harder to manage for the user.
Example of this would be having different attributes for the same CityObject type (which should be consistent for data coming from the same source).Input == CityJSONFeatureThe importer works only on withCityJSONLfiles, which are CityJSON files decomposed into theirfeatures(CityJSONFeature).The easiest way to create these from a CityJSON file is withcjio, by followingthese instructions.The importer supports 3 kinds of input:a single CityJSONL file (only those as the output of cjio currently work)a directory of CityJSONL files (all files withjsonlextensions are located and imported)STDIN using the pipe operator:cat file.jsonl | cjdb ...Coordinate Reference SystemsThecjdbimporter does not allow inconsistent CRSs (coordinate reference systems) within the same database schema. For storing data in different CRSs, you have to create different schemas.The data needs to be either harmonized beforehand, or the--transformflag can be used upon import, to reproject all the geometries to the CRS of the existing schema.
Specifying a 2D CRS (instead of a 3D one) will cause the Z-coordinates to remain unchanged.Note:reprojections slow down the import significantly.Note:Source data with missing"metadata"/"referenceSystem"cannot be reprojected due to unknown source reference system.
You can use the-I/--sridflag to set the SRID of the input file.3D reprojectionspyprojis used for CRS reprojections.
While it supports 3D CRS transformations between different systems, sometimes downloading additionalgridsis required.
The importer will attempt to download the grids needed for the reprojection, with the following message:Attempting to download additional grids required for CRS transformation.
This can also be done manually, and the files should be put in this folder:
        {pyproj_directory}If that fails, the user will have to download the required grids and put them in the printed{pyproj_directory}themselves.CityJSON ExtensionsIfCityJSON Extensionsare present in the imported files, they can be found listed in theextensionscolumn in thecj_metadatatable.TheCityJSON specificationsmention 3 different extendable features, and thecjdbimporter deals with them as follows:Complex attributesNo action is taken. These attributes end up in theattributesJSONB column.Additional root propertiesAdditional root properties are placed in theextra propertiesJSONB column in thecj_metadatatable.Additional CityObject typeAdditional CityObject types are appended to the list of allowed CityJSON objects.CityJSON GeometryTemplateGeometry templatesare resolved for each object geometry, so that the object in the table ends up with its real-world coordinates (instead of vertex references or relative template coordinates).Data validationThe importer does not validate the structure of the file. It is assumed that the input file is schema-valid (CityJSON validator).
It sends out warnings when:CityObject types appear which are defined neither in the main CityJSON specification nor in any of the supplied extensions.the specified target CRS does not have the Z-axis definedthe source dataset does not have a CRS defined at allRepeated object IDsThe importer does not check if an object with a specific ID exists already in the database - every imported object gets and new id. However, at the time of import the importer will detect previously detected files with the same filename. The user can choose to run the import with either the-g, --ignore-repeated-fileoption to import files with the same filename under a different id or--overwriteto overwriteallpreviously imported objects with this filename.ContributorsThis project started as a group project in theMSc Geomatics at TUDelft.
The original code for the project can be foundhere, and the authors were:@cynthiacai56,@LanYan1110,@YitongXia,@Topher2k,@siebren014,@leoleonsioThis version has been improved and will be maintained by@GinaStavropoulou, and@hugoledoux.