MapWindow 4 - ActiveX Control Programming : MapWindow Discussion Forum
I am attempting to develop a simple search-zoom to plugin. I am having a bit of trouble figuring out how to zoom to a shape and/or setting the map view extents to the extents of a shape within a shapefile. Below is my code, the comments do a fairly good job of explaining the problems I am having.
Can't Zoom to Shape or Set View Extents
Posted by: Josh ()
Date: December 21, 2010 12:15PM

I am attempting to develop a simple search-zoom to plugin. I am having a bit of trouble figuring out how to zoom to a shape and/or setting the map view extents to the extents of a shape within a shapefile. Below is my code, the comments do a fairly good job of explaining the problems I am having. Can anyone point me in the right direction? I have spent a few hours searching the forum and I have found many posts about similar issues, but I have not found anything that directly solves the problems I am having.

Thanks,
Josh

 Private Sub ZoomToPcl()
        Dim s As String
        Dim FldI As Integer
        Dim sf As New MapWinGIS.Shapefile
        Dim shp As New MapWinGIS.Shape
        Dim Xmax As Double
        Dim Xmin As Double
        Dim Ymax As Double
        Dim Ymin As Double
        Dim pt As MapWinGIS.Point
        Dim path As MapWinGIS.Shape
        Dim Ilp As Integer

        'Get the parcels Layer 
        If g_MapWin.Layers.IsValidHandle(g_LyrIndx) Then
            g_MapWin.Layers.IsValidHandle(g_LyrIndx)
            'set it the to current layer
            g_MapWin.Layers.CurrentLayer() = g_LyrIndx
            'find the PID field
            sf = g_MapWin.Layers(g_LyrIndx).GetObject
            s = sf.FieldByName("PID").ToString
            'get the field index number and set FLD1 = to the PID index #
            For i = 0 To sf.NumFields - 1
                s = sf.Field(i).Name
                If s = "PID" Then
                    FldI = i
                End If
            Next

            'Loop through all shapes in the parcels layer 
            'see if the sample parcel number 557510300001 is 
            'a valid number in the PID filed 

            For i = 0 To sf.NumShapes - 1
                If sf.CellValue(FldI, i).ToString = "557510300001" Then
                    'we found a shape that has the PID cell value were looking for

                    'Now we want to select that shape and zoom to it 
                    shp = sf.Shape(i)
                    'cant use shp.zoomto unless we use MapWindow.Interfaces.Shape
                    'when we dim shp as MapWindow.Interfaces.Shape
                    'MapWindow gives error
    
                    'cant figure out how to just zoom to the shape 
                    'so lets try to get the extents of the shape and then 
                    'set G_Mapwin.view.extent bounds to the extents of the shape 
                    'were tring to zoom to 

                    Ilp = 0
                    For int As Integer = 0 To sf.Shape(i).numPoints - 1
                        pt = sf.Shape(i).Point(int)
                        If Ilp = 0 Then
                            Xmax = pt.x
                            Ymax = pt.y
                            Xmin = pt.x
                            Ymin = pt.y
                        Else
                            If pt.x > Xmax Then
                                Xmax = pt.x
                            End If
                            If pt.x < Xmin Then
                                Xmin = pt.x
                            End If
                            If pt.y > Ymax Then
                                Ymax = pt.y
                            End If
                            If pt.y < Ymin Then
                                Ymin = pt.y
                            End If
                        End If
                        Ilp = Ilp + 1
                    Next

                    Dim ex As MapWinGIS.Extents
                    ex = g_MapWin.View.Extents
                    MsgBox(ex.xMax)
                    MsgBox(Xmax)
                    Dim zoom As Double

                   
                    'set the map view extent to the extent of the shape 
                    g_MapWin.View.Extents.SetBounds(Xmin, Ymin, 0, Xmax, Ymax, 0)
                    g_MapWin.View.Redraw()
                    g_MapWin.Refresh()
                    'nothing happened

                 
                    zoom = ex.xMax / Xmax
                    MsgBox(zoom)
                    g_MapWin.View.ZoomIn(zoom)
                    'we zoomed in but to the center of the map, not to the 
                    'extents of the shape
                End If
            Next
        End If
        Me.Close()
        g_LyrIndx = Nothing
  End Sub

[Edit:] Added code tags for better reading



Edited 1 time(s). Last edit at 12/22/2010 03:33AM by pmeems.

Options: ReplyQuote
Re: Can't Zoom to Shape or Set View Extents
Posted by: pmeems ()
Date: December 22, 2010 03:41AM

Here is a piece of code to zoom to a specific point:
        /// <summary>Set the map extent to the center of position</summary>
        /// <param name="projectX">Longitude or X coordinate which is spatially referenced</param>
        /// <param name="projectY">Latude or Y coordinate which is spatially referenced</param>
        public void centeringExtent(double projectX, double projectY)
        {
            MapWinGIS.Extents ex = this.mapWin.View.Extents;
            MapWinGIS.Extents newEx = new MapWinGIS.Extents();
            double mpX = (ex.xMax - ex.xMin) / 2;
            double mpY = (ex.yMax - ex.yMin) / 2;

            newEx.SetBounds((projectX - mpX), (projectY - mpY), 0, (projectX + mpX), (projectY + mpY), 0);
            this.mapWin.View.Extents = newEx;
        }

To zoom to a specific shape you can use:
m_MapWin.Layers[0].Shapes[0].ZoomTo();

I hope this helps,

Paul

--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group

Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.

---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS

Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl

*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn



Edited 1 time(s). Last edit at 12/22/2010 03:42AM by pmeems.

Options: ReplyQuote


Sorry, only registered users may post in this forum.





Banner Exchange




GISCP.com




Send us your banner logo (160x120) for the space above, and add this MapWindow banner ad to your site:

Just paste this text in your page: