Ejemplo n.º 1
0
			/// <summary>
			/// Utility method to write shapefiles
			/// </summary>
			/// <param name="filename">Output Shapefile name</param>

			private void writeShape(string filename)
			{
				IntPtr shphandle = ShapeLib.SHPCreate(filename,ShapeLib.ShapeType.PolyLine);
						
				double[] lat = new double[m_multiline.Count];
				double[] lon = new double[m_multiline.Count];
						
				int i=0;
				foreach(MeasureLine line in m_multiline)
				{
					lat[i] = line.StartLatitude.Degrees;
					lon[i] = line.StartLongitude.Degrees;
					i++;
				}
						
				ShapeLib.SHPObject poly = ShapeLib.SHPCreateSimpleObject(ShapeLib.ShapeType.Polygon,m_multiline.Count,lon,lat,null);
				ShapeLib.SHPWriteObject(shphandle,0,poly);
				ShapeLib.SHPDestroyObject(poly);
				ShapeLib.SHPClose(shphandle);
			}
Ejemplo n.º 2
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!");
        }
Ejemplo n.º 3
0
        public bool CreatePointFile(string path, string[] pointJson)
        {
            if (pointJson == null || pointJson.Length == 0)
            {
                return(false);
            }

            if (System.IO.Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            ShapeLib.ShapeType shpType = ShapeLib.ShapeType.Point;

            hShpPoint = ShapeLib.SHPCreate(path + "\\poi", shpType);
            DbfFile   odbf = CreateAttr(path, pointJson);
            DbfRecord orec = new DbfRecord(odbf.Header)
            {
                AllowDecimalTruncate = true
            };


            //逐个点录入属性信息及提取坐标
            for (int i = 0; i < pointJson.Length; i++)
            {
                //取出JSON中的属性信息
                JObject obj       = (JObject)JsonConvert.DeserializeObject(pointJson[i]);
                string  attrs     = obj["attr"].ToString();
                string  attrtypes = obj["attrtype"].ToString();
                string  geometry  = obj["geometry"].ToString();

                //将该点属性信息记录为List<PointInf>
                List <PointInf> pointinf   = new List <PointInf>();
                List <string>   values     = new List <string>();
                List <string>   types      = new List <string>();
                JToken          attrname   = (JToken)JsonConvert.DeserializeObject(attrs);
                int             listLenhth = 0;
                foreach (JProperty jp in attrname)
                {
                    values.Add(jp.Value.ToString());
                    listLenhth++;
                }
                JToken attrtype = (JToken)JsonConvert.DeserializeObject(attrtypes);
                foreach (JProperty jp in attrtype)
                {
                    types.Add(jp.Value.ToString());
                }
                for (int a = 0; a < listLenhth - 1; a++)
                {
                    pointinf.Add(new PointInf(values[a], types[a]));
                }

                try
                {
                    //生成坐标点
                    List <Point> geo    = CreatePointFilePointList(geometry);
                    double[]     xCoord = new double[1];
                    double[]     yCoord = new double[1];
                    xCoord[0] = geo[0].X;
                    yCoord[0] = geo[0].Y;
                    IntPtr pShp = ShapeLib.SHPCreateSimpleObject(shpType, 1, xCoord, yCoord, null);
                    ShapeLib.SHPWriteObject(hShpPoint, -1, pShp);
                    ShapeLib.SHPDestroyObject(pShp);

                    //录入属性信息
                    for (int a = 0; a < listLenhth - 1; a++)
                    {
                        if (pointinf[a].Type == "text")
                        {
                            orec[a] = pointinf[a].Value;
                        }
                        else
                        {
                            string c = pointinf[a].Value.ToString();
                            orec[a] = c;
                        }
                    }
                    odbf.Write(orec, true);
                }
                catch
                {
                    string c = pointinf[0].Value.ToString();
                }
            }
            //关闭
            odbf.Close();
            if (hShpPoint != IntPtr.Zero)
            {
                ShapeLib.SHPClose(hShpPoint);
            }

            return(true);
        }