Some spatial data manipulation acts in Postgres/PostGis

Load only the table names, which have spatial references

        //tables with spatial references are store in geometry_columns table in postgres (after postgis installation)
        DataTable loadTableNames() 
        {
            DataTable dt = new DataTable();
            using (NpgsqlConnection cnn = new NpgsqlConnection(cnsb.ConnectionString))
            {
                NpgsqlDataAdapter da = new NpgsqlDataAdapter("select f_table_name from geometry_columns", cnn);
                try
                {
                    da.Fill(dt);
                }
                catch (ArgumentException asd)
                { MessageBox.Show("Error: " + asd.Message); }
                catch (NpgsqlException fgh)
                { MessageBox.Show("Error: " + fgh.Message); }
            }
            return dt;
        }

Look at some spatial queries
  • Select settlemants, which are in county named 'Pest megye' and sort the result by the name of county and settlemants
  • 
    SELECT county.name, settlemants.name, settlemants.geom FROM county, settlemants 
    WHERE  county.name='PEST MEGYE' 
    AND (SELECT _st_contains(county.geom, settlemants.geom)) 
    ORDER BY county.name, settlemants.name asc 
    
    
  • Select county name and county geometry from tables county and rivers, where the desired name is 'Duna', and county overleps the 'Duna' river
  • 
    SELECT county.name, county.geom FROM county, rivers WHERE rivers.name = 'Duna' AND (SELECT _st_overlaps(county.geom, rivers.geom))
    
    
  • Select settlemants, which are in county named 'Pest megye' and sort the result by the name of county and settlemants, and save the result into
  • 
    SELECT county.name as cname, settlemants.name as bnev, settlemants.geom INTO pest_county FROM county, settlemants 
    WHERE  county.name = 'PEST MEGYE' 
    AND (SELECT _st_contains(county.geom, settlemants.geom)) 
    ORDER BY county.name, settlemants.name asc 
    
    
  • Polygon geometry column creation for parcel database
  • 
    CREATE TABLE IF NOT EXISTS (id BIGSERIAL PRIMARY KEY, codeNumber VARCHAR, geom GEOMETRY(POLYGON,23700))", "table name") 
                 // Explanation: ID column (PK), codeNumber column and geometry column (type, code number of the projection system)
    
    
  • Insert a record, which contains polygon geometry data too
  • 
    INSERT INTO (codeNumber, geometry) SELECT ST_GeomFromText('SRID=23700;POLYGON(({2}))') ", "table name", codeNumber, geometry)); 
                            //Explanation: codeNumber-> string, geomerty->poligon string