C # to create an open source Webgis (ii) Spatial database

Source: Internet
Author: User
Tags postgresql sqlite postgis
Characteristics of spatial database

Spatial database, is to store and analyze spatial data, there are some GIS systems using space files to achieve, such as MapInfo, shape file format, but the spatial database has its unique advantages, in fact, the basic database system and file system features are compared:

1, database design-oriented data model object, database design is based on the data model.

2, the data redundancy of the database system is small, the data share degree is high.

3, the database system data and procedures have a high degree of independence.

4, the database system through DBMS for data security and integrity control.

5, the minimum data Access unit in the database is the data item.

For example, to achieve a database of multiple tables query, a SQL to fix the problem, replaced by file format processing is much more troublesome.

However, it is only a simple database, spatial database more space information storage and analysis capabilities, for simple attribute information, the geographical dimension of the label, so that it has a qualitative difference. Common spatial Database and PostgreSQL

The current common spatial databases are Oracle spatial, Ms Server (2008 or above), PostgreSQL (PostGIS), MySQL, SQLite spatial, where Oracle spatial, MS Server (2008 or more) powerful, but expensive, MySQL, SQLite spatial free, but the space processing capacity is weak, comprehensive and relatively open source PostgreSQL (PostGIS) The best, many GIS projects have broad support, Select it in this series.

PostgreSQL Download Address is: http://www.postgresql.org/, download installation after a wizard to suggest you install some plug-ins, check the PostGIS, others can consider whether or not tick. In fact, PostGIS is a PostgreSQL space plug-in, just like Oracle spatial is an Oracle plug-in.

When the installation is successful, you will find that it has installed the database management client Pgadmin (of course you do not like or use the unused you can use back to navicat), the left directory tree expands as follows:

Space Query

Among them cell_region, Cellcover_region, site_font_points is I use PostGIS to upload the shape file to the PostgreSQL to generate the table, which Spatial_ref_sys is automatically generated, The correlation between spatial data and attribute data is recorded. Well, we're trying to use the spatial query capabilities of it:

Well done, the data came out, the code is very good to understand, St_within is a PG-band space contains functions, meaning that the query Cellcover_region surface layer contains a GID 1 points of the record. Basically done here. C # Call

However, when it comes to C # building, how do you give a sample of C # calls. Here we want to borrow npgsql, search and download, and then create a new WinForm program, the following files to copy in and add references (to remind: may be separated in different directories, I just did not add Mono.Security.dll, get to compile):

Then test the code:

 private void Tryquery () {try {//Postgesql-style connection string String connstring = String.Format ("server={0}; Port={1}; User id={2};
                PASSWORD={3};D Atabase={4}; "," localhost "," 5432 "," Postgres "," sa "," POSTGIS20 "); Making connection with NPGSQL provider Npgsqlconnection conn = new Npgsqlconnection (Connstri
                NG); Conn.
                Open ();
                Quite complex SQL statement//data adapter making request from our connection
                String sql = "SELECT * from Cellcover_region"; String sql = "Select a.* from Public.cellcover_region as a, site_font_point as B where b.gid=1 and St_within (b.geom,a.geom
                )";
                Npgsqldataadapter da = new Npgsqldataadapter (SQL, conn); I always reset DataSet before I do//something with it ... I don ' t know why:-) DS.
                Reset (); The filling DataSet with the result from Npgsqldataadapter da.
                Fill (DS); Since it C # DataSet can handle multiple tables, we'll select the-dt = ds.
                Tables[0];
                Connect grid to DataTable datagridview1.datasource = DT; Since we showing the result we don ' t need connection anymore Conn.
            Close (); The catch (Exception ex) {MessageBox.Show (ex).
            Message.tostring ()); }
        }

Run it, the effect is as shown, and we can see that the database query is consistent:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.