Exemplo n.º 1
0
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string exportlog;

            /*reinitialize shape names lists*/

            txtSave.Clear();
            /*check that at least one type of shape is selected*/
            if ((!cbLines.Checked) && (!cbPoints.Checked) && (!cbPolygons.Checked))
            {
                txtSave.Text = "You need to select at least one type of shape to export";
                return;
            }

            ConversionOptions options = new ConversionOptions();

            options.Lines       = cbLines.Checked;
            options.Points      = cbPoints.Checked;
            options.Polygons    = cbPolygons.Checked;
            options.ConvertTags = cbExtractMetaData.Checked;
            options.Projection  = tbProjection.Text;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // This is to get around a peculiar behaviour of ShapeLib.
                // If the selected file has an extension, it is overwritten,
                // no matter which additions are made to the filename in
                // ConvertToSHP. By stripping the extension and all additions
                // made by previous runs of the program, it is ensured that
                // new *-points, *-lines, and *-polygons files are created.
                string tempName = saveFileDialog1.FileName;
                tempName = tempName.Substring(0, tempName.LastIndexOf("."));
                if (tempName.LastIndexOf("-points") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-points"));
                }
                if (tempName.LastIndexOf("-lines") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-lines"));
                }
                if (tempName.LastIndexOf("-polygons") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-polygons"));
                }
                options.Filename = tempName;
            }
            else
            {
                return;
            }



            exportlog     = export.SaveToShapefile(options);
            txtSave.Text += exportlog;
        }
Exemplo n.º 2
0
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string exportlog;

            /*reinitialize shape names lists*/

            txtSave.Clear();
            /*check that at least one type of shape is selected*/
            if ((!cbLines.Checked) && (!cbPoints.Checked) && (!cbPolygons.Checked))
            {
                txtSave.Text = "You need to select at least one type of shape to export";
                return;
            }

            ConversionOptions options = new ConversionOptions();
            options.Lines = cbLines.Checked;
            options.Points = cbPoints.Checked;
            options.Polygons = cbPolygons.Checked;
            options.ConvertTags = cbExtractMetaData.Checked;
            options.Projection = tbProjection.Text;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // This is to get around a peculiar behaviour of ShapeLib.
                // If the selected file has an extension, it is overwritten,
                // no matter which additions are made to the filename in
                // ConvertToSHP. By stripping the extension and all additions
                // made by previous runs of the program, it is ensured that
                // new *-points, *-lines, and *-polygons files are created.
                string tempName = saveFileDialog1.FileName;
                tempName = tempName.Substring(0, tempName.LastIndexOf("."));
                if (tempName.LastIndexOf("-points") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-points"));
                }
                if (tempName.LastIndexOf("-lines") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-lines"));
                }
                if (tempName.LastIndexOf("-polygons") > 0)
                {
                    tempName = tempName.Substring(0, tempName.LastIndexOf("-polygons"));
                }
                options.Filename = tempName;
            }
            else
                return;

            
            
            exportlog = export.SaveToShapefile(options);
            txtSave.Text += exportlog;

        }
Exemplo n.º 3
0
        public string SaveToShapefile(ConversionOptions options)
        {
            ShapeLib.ShapeType shapetype;
            double[]           x, y;
            IntPtr             hShpPoly = IntPtr.Zero, hShpLine = IntPtr.Zero, hShpPoint = IntPtr.Zero;

            linesData    = new List <MetaData>();
            polygonsData = new List <MetaData>();
            pointsData   = new List <MetaData>();

            if (options.Lines)
            {
                // create a new PolyLines shapefile
                hShpLine = ShapeLib.SHPCreate(options.Filename + "-lines", ShapeLib.ShapeType.PolyLine);
                if (hShpLine.Equals(IntPtr.Zero))
                {
                    return("Cannot create lines file!");
                }
            }

            if (options.Polygons)
            {
                // create a new Polygons shapefile
                hShpPoly = ShapeLib.SHPCreate(options.Filename + "-polygons", ShapeLib.ShapeType.Polygon);
                if (hShpPoly.Equals(IntPtr.Zero))
                {
                    return("Cannot create polygons file!");
                }
            }

            if (options.Points)
            {
                // create a new Points shapefile
                hShpPoint = ShapeLib.SHPCreate(options.Filename + "-points", ShapeLib.ShapeType.Point);
                if (hShpPoint.Equals(IntPtr.Zero))
                {
                    return("Cannot create points file!");
                }
            }

            /*iterate through OSM ways*/
            foreach (way strada in infile.wayCollection)
            {
                /*check if the way is closed, set shape type accordingly*/
                if (strada.ndCollection[0].reff == strada.ndCollection[strada.ndCollection.Count - 1].reff)
                {
                    shapetype = ShapeLib.ShapeType.Polygon;
                }
                else
                {
                    shapetype = ShapeLib.ShapeType.PolyLine;
                }
                x = new double[strada.ndCollection.Count];
                y = new double[strada.ndCollection.Count];

                /*iterate through all the nodes in the way, set x and y coords*/
                int i = 0;
                foreach (nd nod in strada.ndCollection)
                {
                    node nodcomplet = infile.nodeCollection.GetByRef(nod.reff);
                    /*TO DO: Regional settings - to be checked - should be ok now*/
                    x[i]             = double.Parse(nodcomplet.lon, CultureInfo.InvariantCulture);
                    y[i]             = double.Parse(nodcomplet.lat, CultureInfo.InvariantCulture);
                    nodcomplet.InWay = true;
                    i++;
                }

                /*finding way name*/
                MetaData elementData = new MetaData();
                elementData = extractMetaData(strada);

                foreach (tag t in strada.tagCollection)
                {
                    /*just to make sure that all the streets are
                     * put in the polylines file*/
                    if ((t.k == "highway") && (shapetype == ShapeLib.ShapeType.Polygon))
                    {
                        shapetype = ShapeLib.ShapeType.PolyLine;
                        areas--;
                        ways++;
                    }

                    /*exception for circular ways: junction:roundabout
                     * should be marked as polyline, not as polygon*/
                    if ((t.k == "junction") && (t.v == "roundabout") && (shapetype == ShapeLib.ShapeType.Polygon))
                    {
                        shapetype = ShapeLib.ShapeType.PolyLine;
                        areas--;
                        ways++;
                    }
                }

                /*create object, write it to file and destroy it*/
                IntPtr pshpObj = ShapeLib.SHPCreateSimpleObject(shapetype,
                                                                strada.ndCollection.Count, x, y, new double[strada.ndCollection.Count]);
                int iRet;
                if ((shapetype == ShapeLib.ShapeType.PolyLine) && options.Lines)
                {
                    iRet = ShapeLib.SHPWriteObject(hShpLine, -1, pshpObj);
                    /*add shape meta data to correct list*/
                    linesData.Add(elementData);
                }
                else if ((shapetype == ShapeLib.ShapeType.Polygon) && (options.Polygons))
                {
                    iRet = ShapeLib.SHPWriteObject(hShpPoly, -1, pshpObj);
                    /*add shape meta data to correct list*/
                    polygonsData.Add(elementData);
                }
                ShapeLib.SHPDestroyObject(pshpObj);
            }

            if (options.Points)
            {
                /*write the nodes that are not part of any way*/
                foreach (node nod in infile.nodeCollection)
                {
                    if (!nod.InWay)
                    {
                        x = new double[1];
                        y = new double[1];

                        /*TO DO: Regional settings - to be checked*/
                        x[0] = double.Parse(nod.lon, CultureInfo.InvariantCulture);
                        y[0] = double.Parse(nod.lat, CultureInfo.InvariantCulture);

                        /*create object, write it to file and destroy it*/
                        IntPtr pshpObj = ShapeLib.SHPCreateSimpleObject(ShapeLib.ShapeType.Point,
                                                                        1, x, y, new double[1]);
                        int iRet = ShapeLib.SHPWriteObject(hShpPoint, -1, pshpObj);

                        /*finding node meta data*/
                        MetaData elementData = extractMetaData(nod);
                        pointsData.Add(elementData);
                        ShapeLib.SHPDestroyObject(pshpObj);
                    }
                }
            }

            // free resources and write dbf files
            if (options.Polygons)
            {
                ShapeLib.SHPClose(hShpPoly);
                WriteDBF(ShapeLib.ShapeType.Polygon, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-polygons", options.Projection);
            }
            if (options.Lines)
            {
                ShapeLib.SHPClose(hShpLine);
                WriteDBF(ShapeLib.ShapeType.PolyLine, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-lines", options.Projection);
            }
            if (options.Points)
            {
                ShapeLib.SHPClose(hShpPoint);
                WriteDBF(ShapeLib.ShapeType.Point, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-points", options.Projection);
            }
            return("Completed!");
        }
Exemplo n.º 4
0
        public string SaveToShapefile(ConversionOptions options)
        {
            ShapeLib.ShapeType shapetype;
            double[] x, y;
            IntPtr hShpPoly = IntPtr.Zero, hShpLine = IntPtr.Zero, hShpPoint = IntPtr.Zero;

            linesData = new List<MetaData>();
            polygonsData = new List<MetaData>();
            pointsData = new List<MetaData>();
            
            if (options.Lines)
            {
                // create a new PolyLines shapefile
                hShpLine = ShapeLib.SHPCreate(options.Filename + "-lines", ShapeLib.ShapeType.PolyLine);
                if (hShpLine.Equals(IntPtr.Zero))
                    return "Cannot create lines file!";
            }

            if (options.Polygons)
            {
                // create a new Polygons shapefile
                hShpPoly = ShapeLib.SHPCreate(options.Filename + "-polygons", ShapeLib.ShapeType.Polygon);
                if (hShpPoly.Equals(IntPtr.Zero))
                    return "Cannot create polygons file!";
            }

            if (options.Points)
            {
                // create a new Points shapefile
                hShpPoint = ShapeLib.SHPCreate(options.Filename + "-points", ShapeLib.ShapeType.Point);
                if (hShpPoint.Equals(IntPtr.Zero))
                    return "Cannot create points file!";
            }

            /*iterate through OSM ways*/
            foreach (way strada in infile.wayCollection)
            {
                /*check if the way is closed, set shape type accordingly*/
                if (strada.ndCollection[0].reff == strada.ndCollection[strada.ndCollection.Count - 1].reff)
                {
                    shapetype = ShapeLib.ShapeType.Polygon;
                }
                else
                {
                    shapetype = ShapeLib.ShapeType.PolyLine;
                }
                x = new double[strada.ndCollection.Count];
                y = new double[strada.ndCollection.Count];

                /*iterate through all the nodes in the way, set x and y coords*/
                int i = 0;
                foreach (nd nod in strada.ndCollection)
                {
                    node nodcomplet = infile.nodeCollection.GetByRef(nod.reff);
                    /*TO DO: Regional settings - to be checked - should be ok now*/
                    x[i] = double.Parse(nodcomplet.lon, CultureInfo.InvariantCulture);
                    y[i] = double.Parse(nodcomplet.lat, CultureInfo.InvariantCulture);
                    nodcomplet.InWay = true;
                    i++;
                }

                /*finding way name*/
                MetaData elementData = new MetaData();
                elementData = extractMetaData(strada);

                foreach (tag t in strada.tagCollection)
                {

                    /*just to make sure that all the streets are
                     *put in the polylines file*/
                    if ((t.k == "highway") && (shapetype == ShapeLib.ShapeType.Polygon))
                    {
                        shapetype = ShapeLib.ShapeType.PolyLine;
                        areas--;
                        ways++;
                    }

                    /*exception for circular ways: junction:roundabout 
                     * should be marked as polyline, not as polygon*/
                    if ((t.k == "junction") && (t.v == "roundabout") && (shapetype == ShapeLib.ShapeType.Polygon))
                    {
                        shapetype = ShapeLib.ShapeType.PolyLine;
                        areas--;
                        ways++;
                    }
                }

                /*create object, write it to file and destroy it*/
                IntPtr pshpObj = ShapeLib.SHPCreateSimpleObject(shapetype,
                    strada.ndCollection.Count, x, y, new double[strada.ndCollection.Count]);
                int iRet;
                if ((shapetype == ShapeLib.ShapeType.PolyLine) && options.Lines)
                {
                    iRet = ShapeLib.SHPWriteObject(hShpLine, -1, pshpObj);
                    /*add shape meta data to correct list*/
                    linesData.Add(elementData);
                }
                else if ((shapetype == ShapeLib.ShapeType.Polygon) && (options.Polygons))
                {
                    iRet = ShapeLib.SHPWriteObject(hShpPoly, -1, pshpObj);
                    /*add shape meta data to correct list*/
                    polygonsData.Add(elementData);
                }
                ShapeLib.SHPDestroyObject(pshpObj);
            }

            if (options.Points)
            {
                /*write the nodes that are not part of any way*/
                foreach (node nod in infile.nodeCollection)
                {
                    if (!nod.InWay)
                    {

                        x = new double[1];
                        y = new double[1];

                        /*TO DO: Regional settings - to be checked*/
                        x[0] = double.Parse(nod.lon, CultureInfo.InvariantCulture);
                        y[0] = double.Parse(nod.lat, CultureInfo.InvariantCulture);

                        /*create object, write it to file and destroy it*/
                        IntPtr pshpObj = ShapeLib.SHPCreateSimpleObject(ShapeLib.ShapeType.Point,
                            1, x, y, new double[1]);
                        int iRet = ShapeLib.SHPWriteObject(hShpPoint, -1, pshpObj);

                        /*finding node meta data*/
                        MetaData elementData = extractMetaData(nod);
                        pointsData.Add(elementData);
                        ShapeLib.SHPDestroyObject(pshpObj);
                    }
                }
            }

            // free resources and write dbf files
            if (options.Polygons)
            {
                ShapeLib.SHPClose(hShpPoly);
                WriteDBF(ShapeLib.ShapeType.Polygon, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-polygons", options.Projection);
            }
            if (options.Lines)
            {
                ShapeLib.SHPClose(hShpLine);
                WriteDBF(ShapeLib.ShapeType.PolyLine, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-lines", options.Projection);
            }
            if (options.Points)
            {
                ShapeLib.SHPClose(hShpPoint);
                WriteDBF(ShapeLib.ShapeType.Point, options.Filename, options.ConvertTags);
                writeProjectionFile(options.Filename + "-points", options.Projection);
            }
            return "Completed!";
        }