maRci002 / proj4dart

Proj4dart is a Dart library to transform point coordinates from one coordinate system to another, including datum transformations.
MIT License
26 stars 2 forks source link

Custom crs for flutter_map wrong #7

Closed caocuongajax closed 2 years ago

caocuongajax commented 3 years ago

Hello, I have a problem with custom crs in flutter_map. Could you help me?

This is my code:

List<double> getResolutions(double maxX, double minX, int zoom, [double tileSize = 256.0]) {
  var size = (maxX - minX) / tileSize;
  return List.generate(zoom, (z) => size / math.pow(2, z));
}

int r = 6378137;
double _boundsD = r * math.pi;

final Bounds<double> _bounds = Bounds<double>(
  CustomPoint<double>(532687.840000, 1250489.697300),
  CustomPoint<double>(607009.080400, 1345845.903300),
);
final resolutions = getResolutions(607009.080400, 532687.840000, 19);
final maxZoom = (resolutions.length - 1).toDouble();

var def = '+proj=tmerc +lat_0=0 +lon_0=108.25 +k=0.9999 +x_0=500000 +y_0=0 +ellps=WGS84 +towgs84=-191.90441429,-39.30318279,-111.45032835,0.00928836,-0.01975479,0.00427372,0.252906278 +units=m +no_defs';

var epsg9217 = proj4.Projection.add('EPSG:9217', def);

var epsg9217CRS = Proj4Crs.fromFactory(
  code: 'EPSG:9217',
  proj4Projection: epsg9217,
  resolutions: resolutions,
  bounds: _bounds,
  origins: [CustomPoint(-5122600.0, 1.00011E7)],
);

In main FlutterMap:

FlutterMap(
  options: MapOptions(
    crs: Epsg3857(),
  ),
  children: [
                  TileLayerWidget(
                      options: TileLayerOptions(
                        backgroundColor: Colors.transparent,
                        wmsOptions: WMSTileLayerOptions(
                          crs: const Epsg3857()
                          version: '1.1.1',
                          transparent: true,
                          baseUrl: 'https://ows.terrestris.de/osm/service?',
                          layers: ["OSM-WMS"],
                        )
                      ))
  ],
  layers: [
    TileLayerOptions(
                          backgroundColor: Colors.transparent,
                          fastReplace: true,
                          wmsOptions: WMSTileLayerOptions(
                            crs: epsg9217CRS,
                            version: '1.3.0',
                            transparent: true,
                            baseUrl: <wms_server_url>,
                            layers: ['2,3'],
                          ),
                        ))
  ]
)

Layer 'OSM-WMS' is show on my application but layer ('2,3') is not. I console url that generate by WMSTileLayerOptions but BBOX is not correct. Log : 499804.8535484949, 10000988.210334396, 499804.8535484949, 10000988.210334396

In javascript project, I use proj4js and BBOX is correct. Log in javascript: 582600.854078125, 1278053.6005968752, 583345.8244375, 1278798.5709562502 This is code of javascript:

<script src="https://cdn.jsdelivr.net/npm/proj4@2.7.5/dist/proj4-src.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.6.1/build/ol.js"></script>

const newProjCode = 'EPSG:9217';
const def = "+proj=tmerc +lat_0=0 +lon_0=108.25 +k=0.9999 +x_0=500000 +y_0=0 +ellps=WGS84 +towgs84=-191.90441429,-39.30318279,-111.45032835,0.00928836,-0.01975479,0.00427372,0.252906278 +units=m +no_defs";
proj4.defs(newProjCode, def);
ol.proj.proj4.register(proj4);

const projection = new ol.proj.Projection({
            code: 'EPSG:9217',
            extent: [532687.8399999999, 1250489.6973, 607009.0804000003, 1345845.9033000004], // [540286.530000, 1276528.530000, 585024.462000, 1318764.630000],
            units: 'm',
});

Thank you.

P/S: This is my WMS Capabilities:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<WMS_Capabilities xmlns="http://www.opengis.net/wms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:esri_wms="http://www.esri.com/wms" version="1.3.0" xsi:schemaLocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd http://www.esri.com/wms https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?version=1.3.0%26service=WMS%26request=GetSchemaExtension">
<Service>
<Name>
<![CDATA[ WMS ]]>
</Name>
<Title>
<![CDATA[ Test_BD_BDN ]]>
</Title>
<Abstract>WMS</Abstract>
<KeywordList>
<Keyword>
<![CDATA[ ]]>
</Keyword>
</KeywordList>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?"/>
<ContactInformation>
<ContactPersonPrimary>
<ContactPerson>
<![CDATA[ ]]>
</ContactPerson>
<ContactOrganization>
<![CDATA[ ]]>
</ContactOrganization>
</ContactPersonPrimary>
<ContactPosition>
<![CDATA[ ]]>
</ContactPosition>
<ContactAddress>
<AddressType>
<![CDATA[ ]]>
</AddressType>
<Address>
<![CDATA[ ]]>
</Address>
<City>
<![CDATA[ ]]>
</City>
<StateOrProvince>
<![CDATA[ ]]>
</StateOrProvince>
<PostCode>
<![CDATA[ ]]>
</PostCode>
<Country>
<![CDATA[ ]]>
</Country>
</ContactAddress>
<ContactVoiceTelephone>
<![CDATA[ ]]>
</ContactVoiceTelephone>
<ContactFacsimileTelephone>
<![CDATA[ ]]>
</ContactFacsimileTelephone>
<ContactElectronicMailAddress>
<![CDATA[ ]]>
</ContactElectronicMailAddress>
</ContactInformation>
<Fees>
<![CDATA[ ]]>
</Fees>
<AccessConstraints>
<![CDATA[ ]]>
</AccessConstraints>
<MaxWidth>4096</MaxWidth>
<MaxHeight>4096</MaxHeight>
</Service>
<Capability>
<Request>
<GetCapabilities>
<Format>application/vnd.ogc.wms_xml</Format>
<Format>text/xml</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?"/>
</Get>
</HTTP>
</DCPType>
</GetCapabilities>
<GetMap>
<Format>image/bmp</Format>
<Format>image/jpeg</Format>
<Format>image/tiff</Format>
<Format>image/png</Format>
<Format>image/png8</Format>
<Format>image/png24</Format>
<Format>image/png32</Format>
<Format>image/gif</Format>
<Format>image/svg+xml</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?"/>
</Get>
</HTTP>
</DCPType>
</GetMap>
<GetFeatureInfo>
<Format>application/vnd.esri.wms_raw_xml</Format>
<Format>application/vnd.esri.wms_featureinfo_xml</Format>
<Format>application/vnd.ogc.wms_xml</Format>
<Format>application/geojson</Format>
<Format>text/xml</Format>
<Format>text/html</Format>
<Format>text/plain</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?"/>
</Get>
</HTTP>
</DCPType>
</GetFeatureInfo>
<esri_wms:GetStyles>
<Format>application/vnd.ogc.sld+xml</Format>
<DCPType>
<HTTP>
<Get>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?"/>
</Get>
</HTTP>
</DCPType>
</esri_wms:GetStyles>
</Request>
<Exception>
<Format>application/vnd.ogc.se_xml</Format>
<Format>application/vnd.ogc.se_inimage</Format>
<Format>application/vnd.ogc.se_blank</Format>
<Format>text/xml</Format>
<Format>XML</Format>
</Exception>
<Layer>
<Title>
<![CDATA[ Layers ]]>
</Title>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:0</CRS>
<CRS>EPSG:9217</CRS>
<!--  9217 is customized SR = PROJCS["VN2000",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",108.25],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]  -->
<EX_GeographicBoundingBox>
<westBoundLongitude>108.549444</westBoundLongitude>
<eastBoundLongitude>109.233283</eastBoundLongitude>
<southBoundLatitude>11.307092</southBoundLatitude>
<northBoundLatitude>12.170651</northBoundLatitude>
</EX_GeographicBoundingBox>
<BoundingBox CRS="CRS:84" minx="108.549444" miny="11.307092" maxx="109.233283" maxy="12.170651"/>
<BoundingBox CRS="EPSG:4326" minx="11.307092" miny="108.549444" maxx="12.170651" maxy="109.233283"/>
<BoundingBox CRS="EPSG:0" minx="532687.840000" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<BoundingBox CRS="EPSG:9217" minx="532687.840000" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<Layer queryable="1">
<Title>
<![CDATA[ bdn ]]>
</Title>
<Abstract>
<![CDATA[ ]]>
</Abstract>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:0</CRS>
<CRS>EPSG:9217</CRS>
<!--  9217 is customized SR = PROJCS["VN2000",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",108.25],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]  -->
<EX_GeographicBoundingBox>
<westBoundLongitude>108.549444</westBoundLongitude>
<eastBoundLongitude>109.233283</eastBoundLongitude>
<southBoundLatitude>11.307092</southBoundLatitude>
<northBoundLatitude>12.170651</northBoundLatitude>
</EX_GeographicBoundingBox>
<BoundingBox CRS="CRS:84" minx="108.549444" miny="11.307092" maxx="109.233283" maxy="12.170651"/>
<BoundingBox CRS="EPSG:4326" minx="11.307092" miny="108.549444" maxx="12.170651" maxy="109.233283"/>
<BoundingBox CRS="EPSG:0" minx="532687.840000" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<BoundingBox CRS="EPSG:9217" minx="532687.840000" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<Layer queryable="1">
<Name>1</Name>
<Title>
<![CDATA[ Hành chính cấp xã ]]>
</Title>
<Abstract>
<![CDATA[ ]]>
</Abstract>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:0</CRS>
<CRS>EPSG:9217</CRS>
<!--  9217 is customized SR = PROJCS["VN2000",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",108.25],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]  -->
<EX_GeographicBoundingBox>
<westBoundLongitude>108.549444</westBoundLongitude>
<eastBoundLongitude>109.233267</eastBoundLongitude>
<southBoundLatitude>11.307092</southBoundLatitude>
<northBoundLatitude>12.166196</northBoundLatitude>
</EX_GeographicBoundingBox>
<BoundingBox CRS="CRS:84" minx="108.549444" miny="11.307092" maxx="109.233267" maxy="12.166196"/>
<BoundingBox CRS="EPSG:4326" minx="11.307092" miny="108.549444" maxx="12.166196" maxy="109.233267"/>
<BoundingBox CRS="EPSG:0" minx="532687.840000" miny="1250489.700000" maxx="607009.080000" maxy="1345353.090000"/>
<BoundingBox CRS="EPSG:9217" minx="532687.840000" miny="1250489.700000" maxx="607009.080000" maxy="1345353.090000"/>
<Style>
<Name>default</Name>
<Title>1</Title>
<LegendURL width="166" height="144">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?request=GetLegendGraphic%26version=1.3.0%26format=image/png%26layer=1" xlink:type="simple"/>
</LegendURL>
</Style>
</Layer>
<Layer queryable="1">
<Name>2</Name>
<Title>
<![CDATA[ Địa giới NT ]]>
</Title>
<Abstract>
<![CDATA[ ]]>
</Abstract>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:0</CRS>
<CRS>EPSG:9217</CRS>
<!--  9217 is customized SR = PROJCS["VN2000",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",108.25],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]  -->
<EX_GeographicBoundingBox>
<westBoundLongitude>108.549444</westBoundLongitude>
<eastBoundLongitude>109.233283</eastBoundLongitude>
<southBoundLatitude>11.307092</southBoundLatitude>
<northBoundLatitude>12.170651</northBoundLatitude>
</EX_GeographicBoundingBox>
<BoundingBox CRS="CRS:84" minx="108.549444" miny="11.307092" maxx="109.233283" maxy="12.170651"/>
<BoundingBox CRS="EPSG:4326" minx="11.307092" miny="108.549444" maxx="12.170651" maxy="109.233283"/>
<BoundingBox CRS="EPSG:0" minx="532687.840900" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<BoundingBox CRS="EPSG:9217" minx="532687.840900" miny="1250489.697300" maxx="607009.080400" maxy="1345845.903300"/>
<Style>
<Name>default</Name>
<Title>2</Title>
<LegendURL width="124" height="96">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?request=GetLegendGraphic%26version=1.3.0%26format=image/png%26layer=2" xlink:type="simple"/>
</LegendURL>
</Style>
</Layer>
<Layer queryable="1">
<Name>3</Name>
<Title>
<![CDATA[ Trụ sở UBND các cấp ]]>
</Title>
<Abstract>
<![CDATA[ ]]>
</Abstract>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:0</CRS>
<CRS>EPSG:9217</CRS>
<!--  9217 is customized SR = PROJCS["VN2000",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",108.25],PARAMETER["Scale_Factor",0.9999],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]  -->
<EX_GeographicBoundingBox>
<westBoundLongitude>108.690979</westBoundLongitude>
<eastBoundLongitude>109.170210</eastBoundLongitude>
<southBoundLatitude>11.338382</southBoundLatitude>
<northBoundLatitude>12.006145</northBoundLatitude>
</EX_GeographicBoundingBox>
<BoundingBox CRS="CRS:84" minx="108.690979" miny="11.338382" maxx="109.170210" maxy="12.006145"/>
<BoundingBox CRS="EPSG:4326" minx="11.338382" miny="108.690979" maxx="12.006145" maxy="109.170210"/>
<BoundingBox CRS="EPSG:0" minx="548133.108100" miny="1253928.935000" maxx="600205.405400" maxy="1327670.126000"/>
<BoundingBox CRS="EPSG:9217" minx="548133.108100" miny="1253928.935000" maxx="600205.405400" maxy="1327670.126000"/>
<Style>
<Name>default</Name>
<Title>3</Title>
<LegendURL width="136" height="80">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://localhost:6080/arcgis/services/Test_BD/BDN/MapServer/WmsServer?request=GetLegendGraphic%26version=1.3.0%26format=image/png%26layer=3" xlink:type="simple"/>
</LegendURL>
</Style>
<MinScaleDenominator>9449.404762</MinScaleDenominator>
<MaxScaleDenominator>236235.119048</MaxScaleDenominator>
</Layer>
</Layer>
</Layer>
</Capability>
</WMS_Capabilities>
maRci002 commented 2 years ago

Sorry for late response I think you misconfigured the Proj4Crs.fromFactory constructor.

You can totally remove the bounds for testing purpose because it just protects the map to avoid network call if you are outside of bounds, however if it's misconfigured then you might not see any tiles.

You can apply later if you want: https://epsg.io/9217

Projected bounds: -174394.86 926393.71 630638.77 2589269.3

final Bounds<double> _bounds = Bounds<double>(
  CustomPoint<double>(-174394.86, 926393.71),
  CustomPoint<double>(630638.77, 2589269.3),
);

I don't know where did you get origins values, but you can also omit because it just shifts the map origins: [CustomPoint(-5122600.0, 1.00011E7)], Here is how can you set the origin: https://github.com/fleaflet/flutter_map/blob/master/example/lib/pages/custom_crs/Readme.md Which says basicly just use TopLeftCorner which equals to CustomPoint(minx, maxy) and if you have multiple layer you can provide other ones in the list.

resolutions usually the hardest one you can read here: #2 But that trick usually works for mercator only, according to this answer: https://stackoverflow.com/questions/55837719/how-to-figure-out-resolution-array-from-user-inputted-arcgis-projection-data you can use argis server rest endpoint to obtain your resolutions.