Geolocation reader example: Difference between revisions

From Maria GDK Wiki
Jump to navigation Jump to search
(Created page with "To start you need to create a file containing a reader class for your location data. This class must implement the IPlaceNameDataInterfacer. <source lang="csharp">namespace T...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
To start you need to create a file containing a reader class for your location data. This class must implement the IPlaceNameDataInterfacer.
To start you need to create a file containing a reader class for your location data. This class must implement the [http://codedocs.maria.teleplanglobe.com/release/managed/interface_t_p_g_1_1_geo_framework_1_1_location_service_core_1_1_i_place_name_data_interfacer.html IPlaceNameDataInterfacer].


<source lang="csharp">namespace TPG.GeoFramework.LocationServiceCore
<source lang="csharp">namespace TPG.GeoFramework.LocationServiceCore
Line 21: Line 21:
Start implementing ''CreateTables''. ''CreateTables'' is responsible for creating tables used by the GeoLoc service when running placename searches. Mandatory tables are Rtree for spatial searches, FTS for free text search and main table for available placename information.
Start implementing ''CreateTables''. ''CreateTables'' is responsible for creating tables used by the GeoLoc service when running placename searches. Mandatory tables are Rtree for spatial searches, FTS for free text search and main table for available placename information.


Use helper functions in [http://support.teleplanglobe.com/MariaGDKDoc/html/715B3D03.htm IPlaceNameDB] for creating tables in the main database. The main database will be created in advance and sent as a parameter to the readers constructor.
Use helper functions in [http://codedocs.maria.teleplanglobe.com/release/managed/interface_t_p_g_1_1_geo_framework_1_1_location_service_core_1_1_i_place_name_d_b.html IPlaceNameDB] for creating tables in the main database. The main database will be created in advance and sent as a parameter to the readers constructor.


<source lang="csharp">        private readonly IPlaceNameDB _dbCore;
<source lang="csharp">        private readonly IPlaceNameDB _dbCore;

Latest revision as of 19:06, 2 November 2020

To start you need to create a file containing a reader class for your location data. This class must implement the IPlaceNameDataInterfacer.

namespace TPG.GeoFramework.LocationServiceCore
{
    public class GenericReader : IPlaceNameDataInterfacer
    {
        public void Dispose()
        {
        }

        public void CreateTables(string inputFileName)
        {
        }

        public void LoadData(string inputFileName)
        {
        }
    }
}

Start implementing CreateTables. CreateTables is responsible for creating tables used by the GeoLoc service when running placename searches. Mandatory tables are Rtree for spatial searches, FTS for free text search and main table for available placename information.

Use helper functions in IPlaceNameDB for creating tables in the main database. The main database will be created in advance and sent as a parameter to the readers constructor.

        private readonly IPlaceNameDB _dbCore;

        public GenericReader(IPlaceNameDB dbCore)
        {
            _dbCore = dbCore;
        }

        public void CreateTables(string inputFileName)
        {
            _dbCore.CreateRTree();
            _dbCore.CreateFTS();
            _dbCore.CreateMainTables();
        }

Next step is to populate the tables with data from the datasource. This is done in the LoadData function and depends on the input format.
Main steps are:

  • Open input file
  • Start database transaction
  • Process header information if it exists
  • Process data and use the results to populate tables
  • Commit database transaction
  • Optimize data in FTS table
public void LoadData(string inputFileName)
{
  DataFileStream = File.OpenText(inputFileName);
  ProcessHeader(DataFileStream.ReadLine());

  DbTransaction transaction = _dbCore.BeginTransaction();

  var rowId = (int) _dbCore.GetLastRowId("placenames_main"); 
  for (string row = DataFileStream.ReadLine(); 
       row != null; 
       row = DataFileStream.ReadLine())
    ProcessDataRow(row, rowId++);
    
  transaction.Commit();
  _dbCore.OptimizeFTSdata();
}

Example code how to add data to sqlitedatabase:

private void AddMainData(string[] stringData, int rowId)
{
    // Note: column names must match those defined in PlaceNameDbResources
    StringBuilder sb=new StringBuilder();
    sb.Append("insert into placenames_main");
    sb.Append("(rowid, lat, long, feature_class, feature_code, 
              cc1, placename, placename_alt) ");
    sb.Append("values ("+rowId);
    sb.Append(", " + stringData[IdsToIdx["lat"]]);
    sb.Append(", " + stringData[IdsToIdx["long"]]);
    sb.Append(", '" + stringData[IdsToIdx["fc"]] + "'");
    sb.Append(", '" + stringData[IdsToIdx["dsg"]] + "'");
    sb.Append(", '" + stringData[IdsToIdx["cc"]] + "'");

    string placeName = DBCoreUtils.ToDbStringValue(
      stringData[IdsToIdx["full_name"]]);
    string altPlaceName = DBCoreUtils.ToDbStringValue(
      stringData[IdsToIdx["full_name_nd"]]);
    sb.Append("," + placeName);
    sb.Append("," + (altPlaceName!=placeName?altPlaceName:"''") +")");

    _dbCore.DoSQL(sb.ToString());
}